4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/obdclass/lprocfs_status.c
38 * Author: Hariharan Thantry <thantry@users.sourceforge.net>
41 #define DEBUG_SUBSYSTEM S_CLASS
44 #include "../include/obd_class.h"
45 #include "../include/lprocfs_status.h"
46 #include "../include/lustre/lustre_idl.h"
47 #include <linux/seq_file.h>
48 #include <linux/ctype.h>
50 static const char * const obd_connect_names[] = {
64 "join_file(obsolete)",
68 "remote_client_by_force",
80 "alt_checksum_algorithm",
108 int obd_connect_flags2str(char *page, int count, __u64 flags, char *sep)
113 for (i = 0; obd_connect_names[i] != NULL; i++, mask <<= 1) {
115 ret += snprintf(page + ret, count - ret, "%s%s",
116 ret ? sep : "", obd_connect_names[i]);
118 if (flags & ~(mask - 1))
119 ret += snprintf(page + ret, count - ret,
120 "%sunknown flags %#llx",
121 ret ? sep : "", flags & ~(mask - 1));
124 EXPORT_SYMBOL(obd_connect_flags2str);
126 int lprocfs_read_frac_helper(char *buffer, unsigned long count, long val,
129 long decimal_val, frac_val;
135 decimal_val = val / mult;
136 prtn = snprintf(buffer, count, "%ld", decimal_val);
137 frac_val = val % mult;
139 if (prtn < (count - 4) && frac_val > 0) {
141 int i, temp_mult = 1, frac_bits = 0;
143 temp_frac = frac_val * 10;
144 buffer[prtn++] = '.';
145 while (frac_bits < 2 && (temp_frac / mult) < 1) {
146 /* only reserved 2 bits fraction */
147 buffer[prtn++] = '0';
152 * Need to think these cases :
153 * 1. #echo x.00 > /proc/xxx output result : x
154 * 2. #echo x.0x > /proc/xxx output result : x.0x
155 * 3. #echo x.x0 > /proc/xxx output result : x.x
156 * 4. #echo x.xx > /proc/xxx output result : x.xx
157 * Only reserved 2 bits fraction.
159 for (i = 0; i < (5 - prtn); i++)
162 frac_bits = min((int)count - prtn, 3 - frac_bits);
163 prtn += snprintf(buffer + prtn, frac_bits, "%ld",
164 frac_val * temp_mult / mult);
167 while (buffer[prtn] < '1' || buffer[prtn] > '9') {
169 if (buffer[prtn] == '.') {
176 buffer[prtn++] = '\n';
179 EXPORT_SYMBOL(lprocfs_read_frac_helper);
181 int lprocfs_write_frac_helper(const char __user *buffer, unsigned long count,
184 char kernbuf[20], *end, *pbuf;
186 if (count > (sizeof(kernbuf) - 1))
189 if (copy_from_user(kernbuf, buffer, count))
192 kernbuf[count] = '\0';
199 *val = (int)simple_strtoul(pbuf, &end, 10) * mult;
203 if (end != NULL && *end == '.') {
204 int temp_val, pow = 1;
208 if (strlen(pbuf) > 5)
209 pbuf[5] = '\0'; /*only allow 5bits fractional*/
211 temp_val = (int)simple_strtoul(pbuf, &end, 10) * mult;
214 for (i = 0; i < (end - pbuf); i++)
217 *val += temp_val / pow;
222 EXPORT_SYMBOL(lprocfs_write_frac_helper);
224 #if defined (CONFIG_PROC_FS)
226 static int lprocfs_no_percpu_stats;
227 module_param(lprocfs_no_percpu_stats, int, 0644);
228 MODULE_PARM_DESC(lprocfs_no_percpu_stats, "Do not alloc percpu data for lprocfs stats");
230 #define MAX_STRING_SIZE 128
232 int lprocfs_single_release(struct inode *inode, struct file *file)
234 return single_release(inode, file);
236 EXPORT_SYMBOL(lprocfs_single_release);
238 int lprocfs_seq_release(struct inode *inode, struct file *file)
240 return seq_release(inode, file);
242 EXPORT_SYMBOL(lprocfs_seq_release);
244 /* lprocfs API calls */
246 struct proc_dir_entry *lprocfs_add_simple(struct proc_dir_entry *root,
247 char *name, void *data,
248 struct file_operations *fops)
250 struct proc_dir_entry *proc;
253 if (root == NULL || name == NULL || fops == NULL)
254 return ERR_PTR(-EINVAL);
260 proc = proc_create_data(name, mode, root, fops, data);
262 CERROR("LprocFS: No memory to create /proc entry %s", name);
263 return ERR_PTR(-ENOMEM);
267 EXPORT_SYMBOL(lprocfs_add_simple);
269 struct proc_dir_entry *lprocfs_add_symlink(const char *name,
270 struct proc_dir_entry *parent, const char *format, ...)
272 struct proc_dir_entry *entry;
276 if (parent == NULL || format == NULL)
279 OBD_ALLOC_WAIT(dest, MAX_STRING_SIZE + 1);
283 va_start(ap, format);
284 vsnprintf(dest, MAX_STRING_SIZE, format, ap);
287 entry = proc_symlink(name, parent, dest);
289 CERROR("LprocFS: Could not create symbolic link from %s to %s",
292 OBD_FREE(dest, MAX_STRING_SIZE + 1);
295 EXPORT_SYMBOL(lprocfs_add_symlink);
297 static struct file_operations lprocfs_generic_fops = { };
302 * \param root [in] The parent proc entry on which new entry will be added.
303 * \param list [in] Array of proc entries to be added.
304 * \param data [in] The argument to be passed when entries read/write routines
305 * are called through /proc file.
307 * \retval 0 on success
310 int lprocfs_add_vars(struct proc_dir_entry *root, struct lprocfs_vars *list,
313 if (root == NULL || list == NULL)
316 while (list->name != NULL) {
317 struct proc_dir_entry *proc;
320 if (list->proc_mode != 0000) {
321 mode = list->proc_mode;
322 } else if (list->fops) {
323 if (list->fops->read)
325 if (list->fops->write)
328 proc = proc_create_data(list->name, mode, root,
329 list->fops ?: &lprocfs_generic_fops,
337 EXPORT_SYMBOL(lprocfs_add_vars);
339 void lprocfs_remove(struct proc_dir_entry **rooth)
344 EXPORT_SYMBOL(lprocfs_remove);
346 void lprocfs_remove_proc_entry(const char *name, struct proc_dir_entry *parent)
348 LASSERT(parent != NULL);
349 remove_proc_entry(name, parent);
351 EXPORT_SYMBOL(lprocfs_remove_proc_entry);
353 struct proc_dir_entry *lprocfs_register(const char *name,
354 struct proc_dir_entry *parent,
355 struct lprocfs_vars *list, void *data)
357 struct proc_dir_entry *entry;
359 entry = proc_mkdir(name, parent);
361 entry = ERR_PTR(-ENOMEM);
366 int rc = lprocfs_add_vars(entry, list, data);
368 lprocfs_remove(&entry);
375 EXPORT_SYMBOL(lprocfs_register);
377 /* Generic callbacks */
378 int lprocfs_rd_uint(struct seq_file *m, void *data)
380 seq_printf(m, "%u\n", *(unsigned int *)data);
383 EXPORT_SYMBOL(lprocfs_rd_uint);
385 int lprocfs_wr_uint(struct file *file, const char __user *buffer,
386 unsigned long count, void *data)
389 char dummy[MAX_STRING_SIZE + 1], *end;
392 dummy[MAX_STRING_SIZE] = '\0';
393 if (copy_from_user(dummy, buffer, MAX_STRING_SIZE))
396 tmp = simple_strtoul(dummy, &end, 0);
400 *p = (unsigned int)tmp;
403 EXPORT_SYMBOL(lprocfs_wr_uint);
405 int lprocfs_rd_u64(struct seq_file *m, void *data)
407 seq_printf(m, "%llu\n", *(__u64 *)data);
410 EXPORT_SYMBOL(lprocfs_rd_u64);
412 int lprocfs_rd_atomic(struct seq_file *m, void *data)
414 atomic_t *atom = data;
415 LASSERT(atom != NULL);
416 seq_printf(m, "%d\n", atomic_read(atom));
419 EXPORT_SYMBOL(lprocfs_rd_atomic);
421 int lprocfs_wr_atomic(struct file *file, const char __user *buffer,
422 unsigned long count, void *data)
424 atomic_t *atm = data;
428 rc = lprocfs_write_helper(buffer, count, &val);
435 atomic_set(atm, val);
438 EXPORT_SYMBOL(lprocfs_wr_atomic);
440 int lprocfs_rd_uuid(struct seq_file *m, void *data)
442 struct obd_device *obd = data;
444 LASSERT(obd != NULL);
445 seq_printf(m, "%s\n", obd->obd_uuid.uuid);
448 EXPORT_SYMBOL(lprocfs_rd_uuid);
450 int lprocfs_rd_name(struct seq_file *m, void *data)
452 struct obd_device *dev = data;
454 LASSERT(dev != NULL);
455 seq_printf(m, "%s\n", dev->obd_name);
458 EXPORT_SYMBOL(lprocfs_rd_name);
460 int lprocfs_rd_blksize(struct seq_file *m, void *data)
462 struct obd_device *obd = data;
463 struct obd_statfs osfs;
464 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
465 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
468 seq_printf(m, "%u\n", osfs.os_bsize);
472 EXPORT_SYMBOL(lprocfs_rd_blksize);
474 int lprocfs_rd_kbytestotal(struct seq_file *m, void *data)
476 struct obd_device *obd = data;
477 struct obd_statfs osfs;
478 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
479 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
482 __u32 blk_size = osfs.os_bsize >> 10;
483 __u64 result = osfs.os_blocks;
485 while (blk_size >>= 1)
488 seq_printf(m, "%llu\n", result);
493 EXPORT_SYMBOL(lprocfs_rd_kbytestotal);
495 int lprocfs_rd_kbytesfree(struct seq_file *m, void *data)
497 struct obd_device *obd = data;
498 struct obd_statfs osfs;
499 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
500 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
503 __u32 blk_size = osfs.os_bsize >> 10;
504 __u64 result = osfs.os_bfree;
506 while (blk_size >>= 1)
509 seq_printf(m, "%llu\n", result);
514 EXPORT_SYMBOL(lprocfs_rd_kbytesfree);
516 int lprocfs_rd_kbytesavail(struct seq_file *m, void *data)
518 struct obd_device *obd = data;
519 struct obd_statfs osfs;
520 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
521 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
524 __u32 blk_size = osfs.os_bsize >> 10;
525 __u64 result = osfs.os_bavail;
527 while (blk_size >>= 1)
530 seq_printf(m, "%llu\n", result);
535 EXPORT_SYMBOL(lprocfs_rd_kbytesavail);
537 int lprocfs_rd_filestotal(struct seq_file *m, void *data)
539 struct obd_device *obd = data;
540 struct obd_statfs osfs;
541 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
542 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
545 seq_printf(m, "%llu\n", osfs.os_files);
549 EXPORT_SYMBOL(lprocfs_rd_filestotal);
551 int lprocfs_rd_filesfree(struct seq_file *m, void *data)
553 struct obd_device *obd = data;
554 struct obd_statfs osfs;
555 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
556 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
559 seq_printf(m, "%llu\n", osfs.os_ffree);
563 EXPORT_SYMBOL(lprocfs_rd_filesfree);
565 int lprocfs_rd_server_uuid(struct seq_file *m, void *data)
567 struct obd_device *obd = data;
568 struct obd_import *imp;
569 char *imp_state_name = NULL;
571 LASSERT(obd != NULL);
572 LPROCFS_CLIMP_CHECK(obd);
573 imp = obd->u.cli.cl_import;
574 imp_state_name = ptlrpc_import_state_name(imp->imp_state);
575 seq_printf(m, "%s\t%s%s\n",
576 obd2cli_tgt(obd), imp_state_name,
577 imp->imp_deactive ? "\tDEACTIVATED" : "");
579 LPROCFS_CLIMP_EXIT(obd);
583 EXPORT_SYMBOL(lprocfs_rd_server_uuid);
585 int lprocfs_rd_conn_uuid(struct seq_file *m, void *data)
587 struct obd_device *obd = data;
588 struct ptlrpc_connection *conn;
590 LASSERT(obd != NULL);
592 LPROCFS_CLIMP_CHECK(obd);
593 conn = obd->u.cli.cl_import->imp_connection;
594 if (conn && obd->u.cli.cl_import)
595 seq_printf(m, "%s\n", conn->c_remote_uuid.uuid);
597 seq_puts(m, "<none>\n");
599 LPROCFS_CLIMP_EXIT(obd);
603 EXPORT_SYMBOL(lprocfs_rd_conn_uuid);
605 /** add up per-cpu counters */
606 void lprocfs_stats_collect(struct lprocfs_stats *stats, int idx,
607 struct lprocfs_counter *cnt)
609 unsigned int num_entry;
610 struct lprocfs_counter *percpu_cntr;
612 unsigned long flags = 0;
614 memset(cnt, 0, sizeof(*cnt));
617 /* set count to 1 to avoid divide-by-zero errs in callers */
622 cnt->lc_min = LC_MIN_INIT;
624 num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
626 for (i = 0; i < num_entry; i++) {
627 if (stats->ls_percpu[i] == NULL)
629 percpu_cntr = lprocfs_stats_counter_get(stats, i, idx);
631 cnt->lc_count += percpu_cntr->lc_count;
632 cnt->lc_sum += percpu_cntr->lc_sum;
633 if (percpu_cntr->lc_min < cnt->lc_min)
634 cnt->lc_min = percpu_cntr->lc_min;
635 if (percpu_cntr->lc_max > cnt->lc_max)
636 cnt->lc_max = percpu_cntr->lc_max;
637 cnt->lc_sumsquare += percpu_cntr->lc_sumsquare;
640 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
642 EXPORT_SYMBOL(lprocfs_stats_collect);
645 * Append a space separated list of current set flags to str.
647 #define flag2str(flag, first) \
649 if (imp->imp_##flag) \
650 seq_printf(m, "%s" #flag, first ? "" : ", "); \
652 static int obd_import_flags2str(struct obd_import *imp, struct seq_file *m)
656 if (imp->imp_obd->obd_no_recov) {
657 seq_printf(m, "no_recov");
661 flag2str(invalid, first);
663 flag2str(deactive, first);
664 flag2str(replayable, first);
665 flag2str(pingable, first);
670 static void obd_connect_seq_flags2str(struct seq_file *m, __u64 flags, char *sep)
676 for (i = 0; obd_connect_names[i] != NULL; i++, mask <<= 1) {
678 seq_printf(m, "%s%s",
679 first ? sep : "", obd_connect_names[i]);
683 if (flags & ~(mask - 1))
684 seq_printf(m, "%sunknown flags %#llx",
685 first ? sep : "", flags & ~(mask - 1));
688 int lprocfs_rd_import(struct seq_file *m, void *data)
690 struct lprocfs_counter ret;
691 struct lprocfs_counter_header *header;
692 struct obd_device *obd = (struct obd_device *)data;
693 struct obd_import *imp;
694 struct obd_import_conn *conn;
699 LASSERT(obd != NULL);
700 LPROCFS_CLIMP_CHECK(obd);
701 imp = obd->u.cli.cl_import;
712 ptlrpc_import_state_name(imp->imp_state),
713 imp->imp_connect_data.ocd_instance);
714 obd_connect_seq_flags2str(m, imp->imp_connect_data.ocd_connect_flags, ", ");
718 obd_import_flags2str(imp, m);
723 " failover_nids: [");
724 spin_lock(&imp->imp_lock);
726 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
727 seq_printf(m, "%s%s", j ? ", " : "",
728 libcfs_nid2str(conn->oic_conn->c_peer.nid));
733 " current_connection: %s\n"
734 " connection_attempts: %u\n"
736 " in-progress_invalidations: %u\n",
737 imp->imp_connection == NULL ? "<none>" :
738 libcfs_nid2str(imp->imp_connection->c_peer.nid),
741 atomic_read(&imp->imp_inval_count));
742 spin_unlock(&imp->imp_lock);
744 if (obd->obd_svc_stats == NULL)
747 header = &obd->obd_svc_stats->ls_cnt_header[PTLRPC_REQWAIT_CNTR];
748 lprocfs_stats_collect(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR, &ret);
749 if (ret.lc_count != 0) {
750 /* first argument to do_div MUST be __u64 */
751 __u64 sum = ret.lc_sum;
752 do_div(sum, ret.lc_count);
759 " unregistering: %u\n"
761 " avg_waittime: %llu %s\n",
762 atomic_read(&imp->imp_inflight),
763 atomic_read(&imp->imp_unregistering),
764 atomic_read(&imp->imp_timeouts),
765 ret.lc_sum, header->lc_units);
768 for (j = 0; j < IMP_AT_MAX_PORTALS; j++) {
769 if (imp->imp_at.iat_portal[j] == 0)
771 k = max_t(unsigned int, k,
772 at_get(&imp->imp_at.iat_service_estimate[j]));
775 " service_estimates:\n"
776 " services: %u sec\n"
777 " network: %u sec\n",
779 at_get(&imp->imp_at.iat_net_latency));
783 " last_replay: %llu\n"
784 " peer_committed: %llu\n"
785 " last_checked: %llu\n",
786 imp->imp_last_replay_transno,
787 imp->imp_peer_committed_transno,
788 imp->imp_last_transno_checked);
791 for (rw = 0; rw <= 1; rw++) {
792 lprocfs_stats_collect(obd->obd_svc_stats,
793 PTLRPC_LAST_CNTR + BRW_READ_BYTES + rw,
795 if (ret.lc_sum > 0 && ret.lc_count > 0) {
796 /* first argument to do_div MUST be __u64 */
797 __u64 sum = ret.lc_sum;
798 do_div(sum, ret.lc_count);
801 " %s_data_averages:\n"
802 " bytes_per_rpc: %llu\n",
803 rw ? "write" : "read",
807 j = opcode_offset(OST_READ + rw) + EXTRA_MAX_OPCODES;
808 header = &obd->obd_svc_stats->ls_cnt_header[j];
809 lprocfs_stats_collect(obd->obd_svc_stats, j, &ret);
810 if (ret.lc_sum > 0 && ret.lc_count != 0) {
811 /* first argument to do_div MUST be __u64 */
812 __u64 sum = ret.lc_sum;
813 do_div(sum, ret.lc_count);
816 " %s_per_rpc: %llu\n",
817 header->lc_units, ret.lc_sum);
821 " MB_per_sec: %u.%.02u\n",
822 k / j, (100 * k / j) % 100);
827 LPROCFS_CLIMP_EXIT(obd);
830 EXPORT_SYMBOL(lprocfs_rd_import);
832 int lprocfs_rd_state(struct seq_file *m, void *data)
834 struct obd_device *obd = (struct obd_device *)data;
835 struct obd_import *imp;
838 LASSERT(obd != NULL);
839 LPROCFS_CLIMP_CHECK(obd);
840 imp = obd->u.cli.cl_import;
842 seq_printf(m, "current_state: %s\n",
843 ptlrpc_import_state_name(imp->imp_state));
844 seq_printf(m, "state_history:\n");
845 k = imp->imp_state_hist_idx;
846 for (j = 0; j < IMP_STATE_HIST_LEN; j++) {
847 struct import_state_hist *ish =
848 &imp->imp_state_hist[(k + j) % IMP_STATE_HIST_LEN];
849 if (ish->ish_state == 0)
851 seq_printf(m, " - ["CFS_TIME_T", %s]\n",
853 ptlrpc_import_state_name(ish->ish_state));
856 LPROCFS_CLIMP_EXIT(obd);
859 EXPORT_SYMBOL(lprocfs_rd_state);
861 int lprocfs_at_hist_helper(struct seq_file *m, struct adaptive_timeout *at)
864 for (i = 0; i < AT_BINS; i++)
865 seq_printf(m, "%3u ", at->at_hist[i]);
869 EXPORT_SYMBOL(lprocfs_at_hist_helper);
871 /* See also ptlrpc_lprocfs_rd_timeouts */
872 int lprocfs_rd_timeouts(struct seq_file *m, void *data)
874 struct obd_device *obd = (struct obd_device *)data;
875 struct obd_import *imp;
876 unsigned int cur, worst;
881 LASSERT(obd != NULL);
882 LPROCFS_CLIMP_CHECK(obd);
883 imp = obd->u.cli.cl_import;
887 /* Some network health info for kicks */
888 s2dhms(&ts, now - imp->imp_last_reply_time);
889 seq_printf(m, "%-10s : %ld, "DHMS_FMT" ago\n",
890 "last reply", imp->imp_last_reply_time, DHMS_VARS(&ts));
892 cur = at_get(&imp->imp_at.iat_net_latency);
893 worst = imp->imp_at.iat_net_latency.at_worst_ever;
894 worstt = imp->imp_at.iat_net_latency.at_worst_time;
895 s2dhms(&ts, now - worstt);
896 seq_printf(m, "%-10s : cur %3u worst %3u (at %ld, "DHMS_FMT" ago) ",
897 "network", cur, worst, worstt, DHMS_VARS(&ts));
898 lprocfs_at_hist_helper(m, &imp->imp_at.iat_net_latency);
900 for (i = 0; i < IMP_AT_MAX_PORTALS; i++) {
901 if (imp->imp_at.iat_portal[i] == 0)
903 cur = at_get(&imp->imp_at.iat_service_estimate[i]);
904 worst = imp->imp_at.iat_service_estimate[i].at_worst_ever;
905 worstt = imp->imp_at.iat_service_estimate[i].at_worst_time;
906 s2dhms(&ts, now - worstt);
907 seq_printf(m, "portal %-2d : cur %3u worst %3u (at %ld, "
908 DHMS_FMT" ago) ", imp->imp_at.iat_portal[i],
909 cur, worst, worstt, DHMS_VARS(&ts));
910 lprocfs_at_hist_helper(m, &imp->imp_at.iat_service_estimate[i]);
913 LPROCFS_CLIMP_EXIT(obd);
916 EXPORT_SYMBOL(lprocfs_rd_timeouts);
918 int lprocfs_rd_connect_flags(struct seq_file *m, void *data)
920 struct obd_device *obd = data;
923 LPROCFS_CLIMP_CHECK(obd);
924 flags = obd->u.cli.cl_import->imp_connect_data.ocd_connect_flags;
925 seq_printf(m, "flags=%#llx\n", flags);
926 obd_connect_seq_flags2str(m, flags, "\n");
928 LPROCFS_CLIMP_EXIT(obd);
931 EXPORT_SYMBOL(lprocfs_rd_connect_flags);
933 int lprocfs_rd_num_exports(struct seq_file *m, void *data)
935 struct obd_device *obd = data;
937 LASSERT(obd != NULL);
938 seq_printf(m, "%u\n", obd->obd_num_exports);
941 EXPORT_SYMBOL(lprocfs_rd_num_exports);
943 int lprocfs_rd_numrefs(struct seq_file *m, void *data)
945 struct obd_type *class = (struct obd_type *) data;
947 LASSERT(class != NULL);
948 seq_printf(m, "%d\n", class->typ_refcnt);
951 EXPORT_SYMBOL(lprocfs_rd_numrefs);
953 int lprocfs_obd_setup(struct obd_device *obd, struct lprocfs_vars *list)
957 LASSERT(obd != NULL);
958 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
959 LASSERT(obd->obd_type->typ_procroot != NULL);
961 obd->obd_proc_entry = lprocfs_register(obd->obd_name,
962 obd->obd_type->typ_procroot,
964 if (IS_ERR(obd->obd_proc_entry)) {
965 rc = PTR_ERR(obd->obd_proc_entry);
966 CERROR("error %d setting up lprocfs for %s\n",
968 obd->obd_proc_entry = NULL;
972 EXPORT_SYMBOL(lprocfs_obd_setup);
974 int lprocfs_obd_cleanup(struct obd_device *obd)
978 if (obd->obd_proc_exports_entry) {
979 /* Should be no exports left */
980 lprocfs_remove(&obd->obd_proc_exports_entry);
981 obd->obd_proc_exports_entry = NULL;
983 if (obd->obd_proc_entry) {
984 lprocfs_remove(&obd->obd_proc_entry);
985 obd->obd_proc_entry = NULL;
989 EXPORT_SYMBOL(lprocfs_obd_cleanup);
991 static void lprocfs_free_client_stats(struct nid_stat *client_stat)
993 CDEBUG(D_CONFIG, "stat %p - data %p/%p\n", client_stat,
994 client_stat->nid_proc, client_stat->nid_stats);
996 LASSERTF(atomic_read(&client_stat->nid_exp_ref_count) == 0,
997 "nid %s:count %d\n", libcfs_nid2str(client_stat->nid),
998 atomic_read(&client_stat->nid_exp_ref_count));
1000 if (client_stat->nid_proc)
1001 lprocfs_remove(&client_stat->nid_proc);
1003 if (client_stat->nid_stats)
1004 lprocfs_free_stats(&client_stat->nid_stats);
1006 if (client_stat->nid_ldlm_stats)
1007 lprocfs_free_stats(&client_stat->nid_ldlm_stats);
1009 OBD_FREE_PTR(client_stat);
1014 void lprocfs_free_per_client_stats(struct obd_device *obd)
1016 struct cfs_hash *hash = obd->obd_nid_stats_hash;
1017 struct nid_stat *stat;
1019 /* we need extra list - because hash_exit called to early */
1020 /* not need locking because all clients is died */
1021 while (!list_empty(&obd->obd_nid_stats)) {
1022 stat = list_entry(obd->obd_nid_stats.next,
1023 struct nid_stat, nid_list);
1024 list_del_init(&stat->nid_list);
1025 cfs_hash_del(hash, &stat->nid, &stat->nid_hash);
1026 lprocfs_free_client_stats(stat);
1029 EXPORT_SYMBOL(lprocfs_free_per_client_stats);
1031 int lprocfs_stats_alloc_one(struct lprocfs_stats *stats, unsigned int cpuid)
1033 struct lprocfs_counter *cntr;
1034 unsigned int percpusize;
1036 unsigned long flags = 0;
1039 LASSERT(stats->ls_percpu[cpuid] == NULL);
1040 LASSERT((stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) == 0);
1042 percpusize = lprocfs_stats_counter_size(stats);
1043 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[cpuid], percpusize);
1044 if (stats->ls_percpu[cpuid] != NULL) {
1046 if (unlikely(stats->ls_biggest_alloc_num <= cpuid)) {
1047 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1048 spin_lock_irqsave(&stats->ls_lock, flags);
1050 spin_lock(&stats->ls_lock);
1051 if (stats->ls_biggest_alloc_num <= cpuid)
1052 stats->ls_biggest_alloc_num = cpuid + 1;
1053 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1054 spin_unlock_irqrestore(&stats->ls_lock, flags);
1056 spin_unlock(&stats->ls_lock);
1058 /* initialize the ls_percpu[cpuid] non-zero counter */
1059 for (i = 0; i < stats->ls_num; ++i) {
1060 cntr = lprocfs_stats_counter_get(stats, cpuid, i);
1061 cntr->lc_min = LC_MIN_INIT;
1066 EXPORT_SYMBOL(lprocfs_stats_alloc_one);
1068 struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num,
1069 enum lprocfs_stats_flags flags)
1071 struct lprocfs_stats *stats;
1072 unsigned int num_entry;
1073 unsigned int percpusize = 0;
1079 if (lprocfs_no_percpu_stats != 0)
1080 flags |= LPROCFS_STATS_FLAG_NOPERCPU;
1082 if (flags & LPROCFS_STATS_FLAG_NOPERCPU)
1085 num_entry = num_possible_cpus();
1087 /* alloc percpu pointers for all possible cpu slots */
1088 LIBCFS_ALLOC(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1092 stats->ls_num = num;
1093 stats->ls_flags = flags;
1094 spin_lock_init(&stats->ls_lock);
1096 /* alloc num of counter headers */
1097 LIBCFS_ALLOC(stats->ls_cnt_header,
1098 stats->ls_num * sizeof(struct lprocfs_counter_header));
1099 if (stats->ls_cnt_header == NULL)
1102 if ((flags & LPROCFS_STATS_FLAG_NOPERCPU) != 0) {
1103 /* contains only one set counters */
1104 percpusize = lprocfs_stats_counter_size(stats);
1105 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[0], percpusize);
1106 if (stats->ls_percpu[0] == NULL)
1108 stats->ls_biggest_alloc_num = 1;
1109 } else if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0) {
1110 /* alloc all percpu data, currently only obd_memory use this */
1111 for (i = 0; i < num_entry; ++i)
1112 if (lprocfs_stats_alloc_one(stats, i) < 0)
1119 lprocfs_free_stats(&stats);
1122 EXPORT_SYMBOL(lprocfs_alloc_stats);
1124 void lprocfs_free_stats(struct lprocfs_stats **statsh)
1126 struct lprocfs_stats *stats = *statsh;
1127 unsigned int num_entry;
1128 unsigned int percpusize;
1131 if (stats == NULL || stats->ls_num == 0)
1135 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU)
1138 num_entry = num_possible_cpus();
1140 percpusize = lprocfs_stats_counter_size(stats);
1141 for (i = 0; i < num_entry; i++)
1142 if (stats->ls_percpu[i] != NULL)
1143 LIBCFS_FREE(stats->ls_percpu[i], percpusize);
1144 if (stats->ls_cnt_header != NULL)
1145 LIBCFS_FREE(stats->ls_cnt_header, stats->ls_num *
1146 sizeof(struct lprocfs_counter_header));
1147 LIBCFS_FREE(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1149 EXPORT_SYMBOL(lprocfs_free_stats);
1151 void lprocfs_clear_stats(struct lprocfs_stats *stats)
1153 struct lprocfs_counter *percpu_cntr;
1156 unsigned int num_entry;
1157 unsigned long flags = 0;
1159 num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1161 for (i = 0; i < num_entry; i++) {
1162 if (stats->ls_percpu[i] == NULL)
1164 for (j = 0; j < stats->ls_num; j++) {
1165 percpu_cntr = lprocfs_stats_counter_get(stats, i, j);
1166 percpu_cntr->lc_count = 0;
1167 percpu_cntr->lc_min = LC_MIN_INIT;
1168 percpu_cntr->lc_max = 0;
1169 percpu_cntr->lc_sumsquare = 0;
1170 percpu_cntr->lc_sum = 0;
1171 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1172 percpu_cntr->lc_sum_irq = 0;
1176 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1178 EXPORT_SYMBOL(lprocfs_clear_stats);
1180 static ssize_t lprocfs_stats_seq_write(struct file *file,
1181 const char __user *buf,
1182 size_t len, loff_t *off)
1184 struct seq_file *seq = file->private_data;
1185 struct lprocfs_stats *stats = seq->private;
1187 lprocfs_clear_stats(stats);
1192 static void *lprocfs_stats_seq_start(struct seq_file *p, loff_t *pos)
1194 struct lprocfs_stats *stats = p->private;
1196 return (*pos < stats->ls_num) ? pos : NULL;
1199 static void lprocfs_stats_seq_stop(struct seq_file *p, void *v)
1203 static void *lprocfs_stats_seq_next(struct seq_file *p, void *v, loff_t *pos)
1206 return lprocfs_stats_seq_start(p, pos);
1209 /* seq file export of one lprocfs counter */
1210 static int lprocfs_stats_seq_show(struct seq_file *p, void *v)
1212 struct lprocfs_stats *stats = p->private;
1213 struct lprocfs_counter_header *hdr;
1214 struct lprocfs_counter ctr;
1215 int idx = *(loff_t *)v;
1219 do_gettimeofday(&now);
1220 seq_printf(p, "%-25s %lu.%lu secs.usecs\n",
1222 now.tv_sec, (unsigned long)now.tv_usec);
1225 hdr = &stats->ls_cnt_header[idx];
1226 lprocfs_stats_collect(stats, idx, &ctr);
1228 if (ctr.lc_count != 0) {
1229 seq_printf(p, "%-25s %lld samples [%s]",
1230 hdr->lc_name, ctr.lc_count, hdr->lc_units);
1232 if ((hdr->lc_config & LPROCFS_CNTR_AVGMINMAX) &&
1233 (ctr.lc_count > 0)) {
1234 seq_printf(p, " %lld %lld %lld",
1235 ctr.lc_min, ctr.lc_max, ctr.lc_sum);
1236 if (hdr->lc_config & LPROCFS_CNTR_STDDEV)
1237 seq_printf(p, " %lld", ctr.lc_sumsquare);
1245 static const struct seq_operations lprocfs_stats_seq_sops = {
1246 .start = lprocfs_stats_seq_start,
1247 .stop = lprocfs_stats_seq_stop,
1248 .next = lprocfs_stats_seq_next,
1249 .show = lprocfs_stats_seq_show,
1252 static int lprocfs_stats_seq_open(struct inode *inode, struct file *file)
1254 struct seq_file *seq;
1257 rc = seq_open(file, &lprocfs_stats_seq_sops);
1260 seq = file->private_data;
1261 seq->private = PDE_DATA(inode);
1265 struct file_operations lprocfs_stats_seq_fops = {
1266 .owner = THIS_MODULE,
1267 .open = lprocfs_stats_seq_open,
1269 .write = lprocfs_stats_seq_write,
1270 .llseek = seq_lseek,
1271 .release = lprocfs_seq_release,
1274 int lprocfs_register_stats(struct proc_dir_entry *root, const char *name,
1275 struct lprocfs_stats *stats)
1277 struct proc_dir_entry *entry;
1278 LASSERT(root != NULL);
1280 entry = proc_create_data(name, 0644, root,
1281 &lprocfs_stats_seq_fops, stats);
1287 EXPORT_SYMBOL(lprocfs_register_stats);
1289 void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
1290 unsigned conf, const char *name, const char *units)
1292 struct lprocfs_counter_header *header;
1293 struct lprocfs_counter *percpu_cntr;
1294 unsigned long flags = 0;
1296 unsigned int num_cpu;
1298 LASSERT(stats != NULL);
1300 header = &stats->ls_cnt_header[index];
1301 LASSERTF(header != NULL, "Failed to allocate stats header:[%d]%s/%s\n",
1302 index, name, units);
1304 header->lc_config = conf;
1305 header->lc_name = name;
1306 header->lc_units = units;
1308 num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1309 for (i = 0; i < num_cpu; ++i) {
1310 if (stats->ls_percpu[i] == NULL)
1312 percpu_cntr = lprocfs_stats_counter_get(stats, i, index);
1313 percpu_cntr->lc_count = 0;
1314 percpu_cntr->lc_min = LC_MIN_INIT;
1315 percpu_cntr->lc_max = 0;
1316 percpu_cntr->lc_sumsquare = 0;
1317 percpu_cntr->lc_sum = 0;
1318 if ((stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1319 percpu_cntr->lc_sum_irq = 0;
1321 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1323 EXPORT_SYMBOL(lprocfs_counter_init);
1325 #define LPROCFS_OBD_OP_INIT(base, stats, op) \
1327 unsigned int coffset = base + OBD_COUNTER_OFFSET(op); \
1328 LASSERT(coffset < stats->ls_num); \
1329 lprocfs_counter_init(stats, coffset, 0, #op, "reqs"); \
1332 void lprocfs_init_ops_stats(int num_private_stats, struct lprocfs_stats *stats)
1334 LPROCFS_OBD_OP_INIT(num_private_stats, stats, iocontrol);
1335 LPROCFS_OBD_OP_INIT(num_private_stats, stats, get_info);
1336 LPROCFS_OBD_OP_INIT(num_private_stats, stats, set_info_async);
1337 LPROCFS_OBD_OP_INIT(num_private_stats, stats, attach);
1338 LPROCFS_OBD_OP_INIT(num_private_stats, stats, detach);
1339 LPROCFS_OBD_OP_INIT(num_private_stats, stats, setup);
1340 LPROCFS_OBD_OP_INIT(num_private_stats, stats, precleanup);
1341 LPROCFS_OBD_OP_INIT(num_private_stats, stats, cleanup);
1342 LPROCFS_OBD_OP_INIT(num_private_stats, stats, process_config);
1343 LPROCFS_OBD_OP_INIT(num_private_stats, stats, postrecov);
1344 LPROCFS_OBD_OP_INIT(num_private_stats, stats, add_conn);
1345 LPROCFS_OBD_OP_INIT(num_private_stats, stats, del_conn);
1346 LPROCFS_OBD_OP_INIT(num_private_stats, stats, connect);
1347 LPROCFS_OBD_OP_INIT(num_private_stats, stats, reconnect);
1348 LPROCFS_OBD_OP_INIT(num_private_stats, stats, disconnect);
1349 LPROCFS_OBD_OP_INIT(num_private_stats, stats, fid_init);
1350 LPROCFS_OBD_OP_INIT(num_private_stats, stats, fid_fini);
1351 LPROCFS_OBD_OP_INIT(num_private_stats, stats, fid_alloc);
1352 LPROCFS_OBD_OP_INIT(num_private_stats, stats, statfs);
1353 LPROCFS_OBD_OP_INIT(num_private_stats, stats, statfs_async);
1354 LPROCFS_OBD_OP_INIT(num_private_stats, stats, packmd);
1355 LPROCFS_OBD_OP_INIT(num_private_stats, stats, unpackmd);
1356 LPROCFS_OBD_OP_INIT(num_private_stats, stats, preallocate);
1357 LPROCFS_OBD_OP_INIT(num_private_stats, stats, create);
1358 LPROCFS_OBD_OP_INIT(num_private_stats, stats, destroy);
1359 LPROCFS_OBD_OP_INIT(num_private_stats, stats, setattr);
1360 LPROCFS_OBD_OP_INIT(num_private_stats, stats, setattr_async);
1361 LPROCFS_OBD_OP_INIT(num_private_stats, stats, getattr);
1362 LPROCFS_OBD_OP_INIT(num_private_stats, stats, getattr_async);
1363 LPROCFS_OBD_OP_INIT(num_private_stats, stats, adjust_kms);
1364 LPROCFS_OBD_OP_INIT(num_private_stats, stats, preprw);
1365 LPROCFS_OBD_OP_INIT(num_private_stats, stats, commitrw);
1366 LPROCFS_OBD_OP_INIT(num_private_stats, stats, find_cbdata);
1367 LPROCFS_OBD_OP_INIT(num_private_stats, stats, init_export);
1368 LPROCFS_OBD_OP_INIT(num_private_stats, stats, destroy_export);
1369 LPROCFS_OBD_OP_INIT(num_private_stats, stats, import_event);
1370 LPROCFS_OBD_OP_INIT(num_private_stats, stats, notify);
1371 LPROCFS_OBD_OP_INIT(num_private_stats, stats, health_check);
1372 LPROCFS_OBD_OP_INIT(num_private_stats, stats, get_uuid);
1373 LPROCFS_OBD_OP_INIT(num_private_stats, stats, quotacheck);
1374 LPROCFS_OBD_OP_INIT(num_private_stats, stats, quotactl);
1375 LPROCFS_OBD_OP_INIT(num_private_stats, stats, pool_new);
1376 LPROCFS_OBD_OP_INIT(num_private_stats, stats, pool_rem);
1377 LPROCFS_OBD_OP_INIT(num_private_stats, stats, pool_add);
1378 LPROCFS_OBD_OP_INIT(num_private_stats, stats, pool_del);
1379 LPROCFS_OBD_OP_INIT(num_private_stats, stats, getref);
1380 LPROCFS_OBD_OP_INIT(num_private_stats, stats, putref);
1382 EXPORT_SYMBOL(lprocfs_init_ops_stats);
1384 int lprocfs_alloc_obd_stats(struct obd_device *obd, unsigned num_private_stats)
1386 struct lprocfs_stats *stats;
1387 unsigned int num_stats;
1390 LASSERT(obd->obd_stats == NULL);
1391 LASSERT(obd->obd_proc_entry != NULL);
1392 LASSERT(obd->obd_cntr_base == 0);
1394 num_stats = ((int)sizeof(*obd->obd_type->typ_dt_ops) / sizeof(void *)) +
1395 num_private_stats - 1 /* o_owner */;
1396 stats = lprocfs_alloc_stats(num_stats, 0);
1400 lprocfs_init_ops_stats(num_private_stats, stats);
1402 for (i = num_private_stats; i < num_stats; i++) {
1403 /* If this LBUGs, it is likely that an obd
1404 * operation was added to struct obd_ops in
1405 * <obd.h>, and that the corresponding line item
1406 * LPROCFS_OBD_OP_INIT(.., .., opname)
1407 * is missing from the list above. */
1408 LASSERTF(stats->ls_cnt_header[i].lc_name != NULL,
1409 "Missing obd_stat initializer obd_op operation at offset %d.\n",
1410 i - num_private_stats);
1412 rc = lprocfs_register_stats(obd->obd_proc_entry, "stats", stats);
1414 lprocfs_free_stats(&stats);
1416 obd->obd_stats = stats;
1417 obd->obd_cntr_base = num_private_stats;
1421 EXPORT_SYMBOL(lprocfs_alloc_obd_stats);
1423 void lprocfs_free_obd_stats(struct obd_device *obd)
1426 lprocfs_free_stats(&obd->obd_stats);
1428 EXPORT_SYMBOL(lprocfs_free_obd_stats);
1430 #define LPROCFS_MD_OP_INIT(base, stats, op) \
1432 unsigned int coffset = base + MD_COUNTER_OFFSET(op); \
1433 LASSERT(coffset < stats->ls_num); \
1434 lprocfs_counter_init(stats, coffset, 0, #op, "reqs"); \
1437 void lprocfs_init_mps_stats(int num_private_stats, struct lprocfs_stats *stats)
1439 LPROCFS_MD_OP_INIT(num_private_stats, stats, getstatus);
1440 LPROCFS_MD_OP_INIT(num_private_stats, stats, null_inode);
1441 LPROCFS_MD_OP_INIT(num_private_stats, stats, find_cbdata);
1442 LPROCFS_MD_OP_INIT(num_private_stats, stats, close);
1443 LPROCFS_MD_OP_INIT(num_private_stats, stats, create);
1444 LPROCFS_MD_OP_INIT(num_private_stats, stats, done_writing);
1445 LPROCFS_MD_OP_INIT(num_private_stats, stats, enqueue);
1446 LPROCFS_MD_OP_INIT(num_private_stats, stats, getattr);
1447 LPROCFS_MD_OP_INIT(num_private_stats, stats, getattr_name);
1448 LPROCFS_MD_OP_INIT(num_private_stats, stats, intent_lock);
1449 LPROCFS_MD_OP_INIT(num_private_stats, stats, link);
1450 LPROCFS_MD_OP_INIT(num_private_stats, stats, rename);
1451 LPROCFS_MD_OP_INIT(num_private_stats, stats, is_subdir);
1452 LPROCFS_MD_OP_INIT(num_private_stats, stats, setattr);
1453 LPROCFS_MD_OP_INIT(num_private_stats, stats, sync);
1454 LPROCFS_MD_OP_INIT(num_private_stats, stats, readpage);
1455 LPROCFS_MD_OP_INIT(num_private_stats, stats, unlink);
1456 LPROCFS_MD_OP_INIT(num_private_stats, stats, setxattr);
1457 LPROCFS_MD_OP_INIT(num_private_stats, stats, getxattr);
1458 LPROCFS_MD_OP_INIT(num_private_stats, stats, init_ea_size);
1459 LPROCFS_MD_OP_INIT(num_private_stats, stats, get_lustre_md);
1460 LPROCFS_MD_OP_INIT(num_private_stats, stats, free_lustre_md);
1461 LPROCFS_MD_OP_INIT(num_private_stats, stats, set_open_replay_data);
1462 LPROCFS_MD_OP_INIT(num_private_stats, stats, clear_open_replay_data);
1463 LPROCFS_MD_OP_INIT(num_private_stats, stats, set_lock_data);
1464 LPROCFS_MD_OP_INIT(num_private_stats, stats, lock_match);
1465 LPROCFS_MD_OP_INIT(num_private_stats, stats, cancel_unused);
1466 LPROCFS_MD_OP_INIT(num_private_stats, stats, renew_capa);
1467 LPROCFS_MD_OP_INIT(num_private_stats, stats, unpack_capa);
1468 LPROCFS_MD_OP_INIT(num_private_stats, stats, get_remote_perm);
1469 LPROCFS_MD_OP_INIT(num_private_stats, stats, intent_getattr_async);
1470 LPROCFS_MD_OP_INIT(num_private_stats, stats, revalidate_lock);
1472 EXPORT_SYMBOL(lprocfs_init_mps_stats);
1474 int lprocfs_alloc_md_stats(struct obd_device *obd,
1475 unsigned num_private_stats)
1477 struct lprocfs_stats *stats;
1478 unsigned int num_stats;
1481 LASSERT(obd->md_stats == NULL);
1482 LASSERT(obd->obd_proc_entry != NULL);
1483 LASSERT(obd->md_cntr_base == 0);
1485 num_stats = 1 + MD_COUNTER_OFFSET(revalidate_lock) +
1487 stats = lprocfs_alloc_stats(num_stats, 0);
1491 lprocfs_init_mps_stats(num_private_stats, stats);
1493 for (i = num_private_stats; i < num_stats; i++) {
1494 if (stats->ls_cnt_header[i].lc_name == NULL) {
1495 CERROR("Missing md_stat initializer md_op operation at offset %d. Aborting.\n",
1496 i - num_private_stats);
1500 rc = lprocfs_register_stats(obd->obd_proc_entry, "md_stats", stats);
1502 lprocfs_free_stats(&stats);
1504 obd->md_stats = stats;
1505 obd->md_cntr_base = num_private_stats;
1509 EXPORT_SYMBOL(lprocfs_alloc_md_stats);
1511 void lprocfs_free_md_stats(struct obd_device *obd)
1513 struct lprocfs_stats *stats = obd->md_stats;
1515 if (stats != NULL) {
1516 obd->md_stats = NULL;
1517 obd->md_cntr_base = 0;
1518 lprocfs_free_stats(&stats);
1521 EXPORT_SYMBOL(lprocfs_free_md_stats);
1523 void lprocfs_init_ldlm_stats(struct lprocfs_stats *ldlm_stats)
1525 lprocfs_counter_init(ldlm_stats,
1526 LDLM_ENQUEUE - LDLM_FIRST_OPC,
1527 0, "ldlm_enqueue", "reqs");
1528 lprocfs_counter_init(ldlm_stats,
1529 LDLM_CONVERT - LDLM_FIRST_OPC,
1530 0, "ldlm_convert", "reqs");
1531 lprocfs_counter_init(ldlm_stats,
1532 LDLM_CANCEL - LDLM_FIRST_OPC,
1533 0, "ldlm_cancel", "reqs");
1534 lprocfs_counter_init(ldlm_stats,
1535 LDLM_BL_CALLBACK - LDLM_FIRST_OPC,
1536 0, "ldlm_bl_callback", "reqs");
1537 lprocfs_counter_init(ldlm_stats,
1538 LDLM_CP_CALLBACK - LDLM_FIRST_OPC,
1539 0, "ldlm_cp_callback", "reqs");
1540 lprocfs_counter_init(ldlm_stats,
1541 LDLM_GL_CALLBACK - LDLM_FIRST_OPC,
1542 0, "ldlm_gl_callback", "reqs");
1544 EXPORT_SYMBOL(lprocfs_init_ldlm_stats);
1546 int lprocfs_exp_print_uuid(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1547 struct hlist_node *hnode, void *data)
1550 struct obd_export *exp = cfs_hash_object(hs, hnode);
1551 struct seq_file *m = (struct seq_file *)data;
1553 if (exp->exp_nid_stats)
1554 seq_printf(m, "%s\n", obd_uuid2str(&exp->exp_client_uuid));
1560 lproc_exp_uuid_seq_show(struct seq_file *m, void *unused)
1562 struct nid_stat *stats = (struct nid_stat *)m->private;
1563 struct obd_device *obd = stats->nid_obd;
1565 cfs_hash_for_each_key(obd->obd_nid_hash, &stats->nid,
1566 lprocfs_exp_print_uuid, m);
1570 LPROC_SEQ_FOPS_RO(lproc_exp_uuid);
1572 struct exp_hash_cb_data {
1577 int lprocfs_exp_print_hash(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1578 struct hlist_node *hnode, void *cb_data)
1581 struct exp_hash_cb_data *data = (struct exp_hash_cb_data *)cb_data;
1582 struct obd_export *exp = cfs_hash_object(hs, hnode);
1584 if (exp->exp_lock_hash != NULL) {
1586 cfs_hash_debug_header(data->m);
1587 data->first = false;
1589 cfs_hash_debug_str(hs, data->m);
1596 lproc_exp_hash_seq_show(struct seq_file *m, void *unused)
1598 struct nid_stat *stats = (struct nid_stat *)m->private;
1599 struct obd_device *obd = stats->nid_obd;
1600 struct exp_hash_cb_data cb_data = {
1605 cfs_hash_for_each_key(obd->obd_nid_hash, &stats->nid,
1606 lprocfs_exp_print_hash, &cb_data);
1610 LPROC_SEQ_FOPS_RO(lproc_exp_hash);
1612 int lprocfs_nid_stats_clear_read(struct seq_file *m, void *data)
1614 seq_printf(m, "%s\n",
1615 "Write into this file to clear all nid stats and stale nid entries");
1618 EXPORT_SYMBOL(lprocfs_nid_stats_clear_read);
1620 static int lprocfs_nid_stats_clear_write_cb(void *obj, void *data)
1622 struct nid_stat *stat = obj;
1624 CDEBUG(D_INFO, "refcnt %d\n", atomic_read(&stat->nid_exp_ref_count));
1625 if (atomic_read(&stat->nid_exp_ref_count) == 1) {
1626 /* object has only hash references. */
1627 spin_lock(&stat->nid_obd->obd_nid_lock);
1628 list_move(&stat->nid_list, data);
1629 spin_unlock(&stat->nid_obd->obd_nid_lock);
1632 /* we has reference to object - only clear data*/
1633 if (stat->nid_stats)
1634 lprocfs_clear_stats(stat->nid_stats);
1639 int lprocfs_nid_stats_clear_write(struct file *file, const char *buffer,
1640 unsigned long count, void *data)
1642 struct obd_device *obd = (struct obd_device *)data;
1643 struct nid_stat *client_stat;
1644 LIST_HEAD(free_list);
1646 cfs_hash_cond_del(obd->obd_nid_stats_hash,
1647 lprocfs_nid_stats_clear_write_cb, &free_list);
1649 while (!list_empty(&free_list)) {
1650 client_stat = list_entry(free_list.next, struct nid_stat,
1652 list_del_init(&client_stat->nid_list);
1653 lprocfs_free_client_stats(client_stat);
1658 EXPORT_SYMBOL(lprocfs_nid_stats_clear_write);
1660 int lprocfs_exp_setup(struct obd_export *exp, lnet_nid_t *nid, int *newnid)
1662 struct nid_stat *new_stat, *old_stat;
1663 struct obd_device *obd = NULL;
1664 struct proc_dir_entry *entry;
1665 char *buffer = NULL;
1670 if (!exp || !exp->exp_obd || !exp->exp_obd->obd_proc_exports_entry ||
1671 !exp->exp_obd->obd_nid_stats_hash)
1674 /* not test against zero because eric say:
1675 * You may only test nid against another nid, or LNET_NID_ANY.
1676 * Anything else is nonsense.*/
1677 if (!nid || *nid == LNET_NID_ANY)
1682 CDEBUG(D_CONFIG, "using hash %p\n", obd->obd_nid_stats_hash);
1684 OBD_ALLOC_PTR(new_stat);
1685 if (new_stat == NULL)
1688 new_stat->nid = *nid;
1689 new_stat->nid_obd = exp->exp_obd;
1690 /* we need set default refcount to 1 to balance obd_disconnect */
1691 atomic_set(&new_stat->nid_exp_ref_count, 1);
1693 old_stat = cfs_hash_findadd_unique(obd->obd_nid_stats_hash,
1694 nid, &new_stat->nid_hash);
1695 CDEBUG(D_INFO, "Found stats %p for nid %s - ref %d\n",
1696 old_stat, libcfs_nid2str(*nid),
1697 atomic_read(&new_stat->nid_exp_ref_count));
1699 /* We need to release old stats because lprocfs_exp_cleanup() hasn't
1700 * been and will never be called. */
1701 if (exp->exp_nid_stats) {
1702 nidstat_putref(exp->exp_nid_stats);
1703 exp->exp_nid_stats = NULL;
1706 /* Return -EALREADY here so that we know that the /proc
1707 * entry already has been created */
1708 if (old_stat != new_stat) {
1709 exp->exp_nid_stats = old_stat;
1713 /* not found - create */
1714 OBD_ALLOC(buffer, LNET_NIDSTR_SIZE);
1715 if (buffer == NULL) {
1720 memcpy(buffer, libcfs_nid2str(*nid), LNET_NIDSTR_SIZE);
1721 new_stat->nid_proc = lprocfs_register(buffer,
1722 obd->obd_proc_exports_entry,
1724 OBD_FREE(buffer, LNET_NIDSTR_SIZE);
1726 if (IS_ERR(new_stat->nid_proc)) {
1727 CERROR("Error making export directory for nid %s\n",
1728 libcfs_nid2str(*nid));
1729 rc = PTR_ERR(new_stat->nid_proc);
1730 new_stat->nid_proc = NULL;
1731 goto destroy_new_ns;
1734 entry = lprocfs_add_simple(new_stat->nid_proc, "uuid",
1735 new_stat, &lproc_exp_uuid_fops);
1736 if (IS_ERR(entry)) {
1737 CWARN("Error adding the NID stats file\n");
1738 rc = PTR_ERR(entry);
1739 goto destroy_new_ns;
1742 entry = lprocfs_add_simple(new_stat->nid_proc, "hash",
1743 new_stat, &lproc_exp_hash_fops);
1744 if (IS_ERR(entry)) {
1745 CWARN("Error adding the hash file\n");
1746 rc = PTR_ERR(entry);
1747 goto destroy_new_ns;
1750 exp->exp_nid_stats = new_stat;
1752 /* protect competitive add to list, not need locking on destroy */
1753 spin_lock(&obd->obd_nid_lock);
1754 list_add(&new_stat->nid_list, &obd->obd_nid_stats);
1755 spin_unlock(&obd->obd_nid_lock);
1760 if (new_stat->nid_proc != NULL)
1761 lprocfs_remove(&new_stat->nid_proc);
1762 cfs_hash_del(obd->obd_nid_stats_hash, nid, &new_stat->nid_hash);
1765 nidstat_putref(new_stat);
1766 OBD_FREE_PTR(new_stat);
1769 EXPORT_SYMBOL(lprocfs_exp_setup);
1771 int lprocfs_exp_cleanup(struct obd_export *exp)
1773 struct nid_stat *stat = exp->exp_nid_stats;
1775 if (!stat || !exp->exp_obd)
1778 nidstat_putref(exp->exp_nid_stats);
1779 exp->exp_nid_stats = NULL;
1783 EXPORT_SYMBOL(lprocfs_exp_cleanup);
1785 __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
1786 struct lprocfs_counter_header *header,
1787 enum lprocfs_stats_flags flags,
1788 enum lprocfs_fields_flags field)
1792 if (lc == NULL || header == NULL)
1796 case LPROCFS_FIELDS_FLAGS_CONFIG:
1797 ret = header->lc_config;
1799 case LPROCFS_FIELDS_FLAGS_SUM:
1801 if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1802 ret += lc->lc_sum_irq;
1804 case LPROCFS_FIELDS_FLAGS_MIN:
1807 case LPROCFS_FIELDS_FLAGS_MAX:
1810 case LPROCFS_FIELDS_FLAGS_AVG:
1811 ret = (lc->lc_max - lc->lc_min) / 2;
1813 case LPROCFS_FIELDS_FLAGS_SUMSQUARE:
1814 ret = lc->lc_sumsquare;
1816 case LPROCFS_FIELDS_FLAGS_COUNT:
1825 EXPORT_SYMBOL(lprocfs_read_helper);
1827 int lprocfs_write_helper(const char __user *buffer, unsigned long count,
1830 return lprocfs_write_frac_helper(buffer, count, val, 1);
1832 EXPORT_SYMBOL(lprocfs_write_helper);
1834 int lprocfs_seq_read_frac_helper(struct seq_file *m, long val, int mult)
1836 long decimal_val, frac_val;
1838 decimal_val = val / mult;
1839 seq_printf(m, "%ld", decimal_val);
1840 frac_val = val % mult;
1847 /* Three cases: x0, xx, 0x */
1848 if ((frac_val % 10) != 0)
1849 seq_printf(m, ".%ld", frac_val);
1851 seq_printf(m, ".%ld", frac_val / 10);
1854 seq_printf(m, "\n");
1857 EXPORT_SYMBOL(lprocfs_seq_read_frac_helper);
1859 int lprocfs_write_u64_helper(const char __user *buffer, unsigned long count,
1862 return lprocfs_write_frac_u64_helper(buffer, count, val, 1);
1864 EXPORT_SYMBOL(lprocfs_write_u64_helper);
1866 int lprocfs_write_frac_u64_helper(const char *buffer, unsigned long count,
1867 __u64 *val, int mult)
1869 char kernbuf[22], *end, *pbuf;
1870 __u64 whole, frac = 0, units;
1871 unsigned frac_d = 1;
1874 if (count > (sizeof(kernbuf) - 1))
1877 if (copy_from_user(kernbuf, buffer, count))
1880 kernbuf[count] = '\0';
1887 whole = simple_strtoull(pbuf, &end, 10);
1895 /* need to limit frac_d to a __u32 */
1896 if (strlen(pbuf) > 10)
1899 frac = simple_strtoull(pbuf, &end, 10);
1900 /* count decimal places */
1901 for (i = 0; i < (end - pbuf); i++)
1906 switch (tolower(*end)) {
1918 /* Specified units override the multiplier */
1923 do_div(frac, frac_d);
1924 *val = sign * (whole * mult + frac);
1927 EXPORT_SYMBOL(lprocfs_write_frac_u64_helper);
1929 static char *lprocfs_strnstr(const char *s1, const char *s2, size_t len)
1938 if (!memcmp(s1, s2, l2))
1946 * Find the string \a name in the input \a buffer, and return a pointer to the
1947 * value immediately following \a name, reducing \a count appropriately.
1948 * If \a name is not found the original \a buffer is returned.
1950 char *lprocfs_find_named_value(const char *buffer, const char *name,
1954 size_t buflen = *count;
1956 /* there is no strnstr() in rhel5 and ubuntu kernels */
1957 val = lprocfs_strnstr(buffer, name, buflen);
1959 return (char *)buffer;
1961 val += strlen(name); /* skip prefix */
1962 while (val < buffer + buflen && isspace(*val)) /* skip separator */
1966 while (val < buffer + buflen && isalnum(*val)) {
1971 return val - *count;
1973 EXPORT_SYMBOL(lprocfs_find_named_value);
1975 int lprocfs_seq_create(struct proc_dir_entry *parent,
1978 const struct file_operations *seq_fops,
1981 struct proc_dir_entry *entry;
1983 /* Disallow secretly (un)writable entries. */
1984 LASSERT((seq_fops->write == NULL) == ((mode & 0222) == 0));
1985 entry = proc_create_data(name, mode, parent, seq_fops, data);
1992 EXPORT_SYMBOL(lprocfs_seq_create);
1994 int lprocfs_obd_seq_create(struct obd_device *dev,
1997 const struct file_operations *seq_fops,
2000 return lprocfs_seq_create(dev->obd_proc_entry, name,
2001 mode, seq_fops, data);
2003 EXPORT_SYMBOL(lprocfs_obd_seq_create);
2005 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value)
2007 if (value >= OBD_HIST_MAX)
2008 value = OBD_HIST_MAX - 1;
2010 spin_lock(&oh->oh_lock);
2011 oh->oh_buckets[value]++;
2012 spin_unlock(&oh->oh_lock);
2014 EXPORT_SYMBOL(lprocfs_oh_tally);
2016 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value)
2020 for (val = 0; ((1 << val) < value) && (val <= OBD_HIST_MAX); val++)
2023 lprocfs_oh_tally(oh, val);
2025 EXPORT_SYMBOL(lprocfs_oh_tally_log2);
2027 unsigned long lprocfs_oh_sum(struct obd_histogram *oh)
2029 unsigned long ret = 0;
2032 for (i = 0; i < OBD_HIST_MAX; i++)
2033 ret += oh->oh_buckets[i];
2036 EXPORT_SYMBOL(lprocfs_oh_sum);
2038 void lprocfs_oh_clear(struct obd_histogram *oh)
2040 spin_lock(&oh->oh_lock);
2041 memset(oh->oh_buckets, 0, sizeof(oh->oh_buckets));
2042 spin_unlock(&oh->oh_lock);
2044 EXPORT_SYMBOL(lprocfs_oh_clear);
2046 int lprocfs_obd_rd_max_pages_per_rpc(struct seq_file *m, void *data)
2048 struct obd_device *dev = data;
2049 struct client_obd *cli = &dev->u.cli;
2051 client_obd_list_lock(&cli->cl_loi_list_lock);
2052 seq_printf(m, "%d\n", cli->cl_max_pages_per_rpc);
2053 client_obd_list_unlock(&cli->cl_loi_list_lock);
2057 EXPORT_SYMBOL(lprocfs_obd_rd_max_pages_per_rpc);