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 <obd_class.h>
45 #include <lprocfs_status.h>
46 #include <lustre/lustre_idl.h>
47 #include <linux/seq_file.h>
51 static int lprocfs_no_percpu_stats = 0;
52 CFS_MODULE_PARM(lprocfs_no_percpu_stats, "i", int, 0644,
53 "Do not alloc percpu data for lprocfs stats");
55 #define MAX_STRING_SIZE 128
57 int lprocfs_single_release(struct inode *inode, struct file *file)
59 return single_release(inode, file);
61 EXPORT_SYMBOL(lprocfs_single_release);
63 int lprocfs_seq_release(struct inode *inode, struct file *file)
65 return seq_release(inode, file);
67 EXPORT_SYMBOL(lprocfs_seq_release);
69 /* lprocfs API calls */
71 struct proc_dir_entry *lprocfs_add_simple(struct proc_dir_entry *root,
72 char *name, void *data,
73 struct file_operations *fops)
75 struct proc_dir_entry *proc;
78 if (root == NULL || name == NULL || fops == NULL)
79 return ERR_PTR(-EINVAL);
85 proc = proc_create_data(name, mode, root, fops, data);
87 CERROR("LprocFS: No memory to create /proc entry %s", name);
88 return ERR_PTR(-ENOMEM);
92 EXPORT_SYMBOL(lprocfs_add_simple);
94 struct proc_dir_entry *lprocfs_add_symlink(const char *name,
95 struct proc_dir_entry *parent, const char *format, ...)
97 struct proc_dir_entry *entry;
101 if (parent == NULL || format == NULL)
104 OBD_ALLOC_WAIT(dest, MAX_STRING_SIZE + 1);
108 va_start(ap, format);
109 vsnprintf(dest, MAX_STRING_SIZE, format, ap);
112 entry = proc_symlink(name, parent, dest);
114 CERROR("LprocFS: Could not create symbolic link from %s to %s",
117 OBD_FREE(dest, MAX_STRING_SIZE + 1);
120 EXPORT_SYMBOL(lprocfs_add_symlink);
122 static struct file_operations lprocfs_generic_fops = { };
127 * \param root [in] The parent proc entry on which new entry will be added.
128 * \param list [in] Array of proc entries to be added.
129 * \param data [in] The argument to be passed when entries read/write routines
130 * are called through /proc file.
132 * \retval 0 on success
135 int lprocfs_add_vars(struct proc_dir_entry *root, struct lprocfs_vars *list,
138 if (root == NULL || list == NULL)
141 while (list->name != NULL) {
142 struct proc_dir_entry *proc;
145 if (list->proc_mode != 0000) {
146 mode = list->proc_mode;
147 } else if (list->fops) {
148 if (list->fops->read)
150 if (list->fops->write)
153 proc = proc_create_data(list->name, mode, root,
154 list->fops ?: &lprocfs_generic_fops,
162 EXPORT_SYMBOL(lprocfs_add_vars);
164 void lprocfs_remove(struct proc_dir_entry **rooth)
169 EXPORT_SYMBOL(lprocfs_remove);
171 void lprocfs_remove_proc_entry(const char *name, struct proc_dir_entry *parent)
173 LASSERT(parent != NULL);
174 remove_proc_entry(name, parent);
176 EXPORT_SYMBOL(lprocfs_remove_proc_entry);
178 struct proc_dir_entry *lprocfs_register(const char *name,
179 struct proc_dir_entry *parent,
180 struct lprocfs_vars *list, void *data)
182 struct proc_dir_entry *entry;
184 entry = proc_mkdir(name, parent);
186 GOTO(out, entry = ERR_PTR(-ENOMEM));
189 int rc = lprocfs_add_vars(entry, list, data);
191 lprocfs_remove(&entry);
198 EXPORT_SYMBOL(lprocfs_register);
200 /* Generic callbacks */
201 int lprocfs_rd_uint(struct seq_file *m, void *data)
203 return seq_printf(m, "%u\n", *(unsigned int *)data);
205 EXPORT_SYMBOL(lprocfs_rd_uint);
207 int lprocfs_wr_uint(struct file *file, const char __user *buffer,
208 unsigned long count, void *data)
211 char dummy[MAX_STRING_SIZE + 1], *end;
214 dummy[MAX_STRING_SIZE] = '\0';
215 if (copy_from_user(dummy, buffer, MAX_STRING_SIZE))
218 tmp = simple_strtoul(dummy, &end, 0);
222 *p = (unsigned int)tmp;
225 EXPORT_SYMBOL(lprocfs_wr_uint);
227 int lprocfs_rd_u64(struct seq_file *m, void *data)
229 return seq_printf(m, LPU64"\n", *(__u64 *)data);
231 EXPORT_SYMBOL(lprocfs_rd_u64);
233 int lprocfs_rd_atomic(struct seq_file *m, void *data)
235 atomic_t *atom = data;
236 LASSERT(atom != NULL);
237 return seq_printf(m, "%d\n", atomic_read(atom));
239 EXPORT_SYMBOL(lprocfs_rd_atomic);
241 int lprocfs_wr_atomic(struct file *file, const char __user *buffer,
242 unsigned long count, void *data)
244 atomic_t *atm = data;
248 rc = lprocfs_write_helper(buffer, count, &val);
255 atomic_set(atm, val);
258 EXPORT_SYMBOL(lprocfs_wr_atomic);
260 int lprocfs_rd_uuid(struct seq_file *m, void *data)
262 struct obd_device *obd = data;
264 LASSERT(obd != NULL);
265 return seq_printf(m, "%s\n", obd->obd_uuid.uuid);
267 EXPORT_SYMBOL(lprocfs_rd_uuid);
269 int lprocfs_rd_name(struct seq_file *m, void *data)
271 struct obd_device *dev = data;
273 LASSERT(dev != NULL);
274 return seq_printf(m, "%s\n", dev->obd_name);
276 EXPORT_SYMBOL(lprocfs_rd_name);
278 int lprocfs_rd_blksize(struct seq_file *m, void *data)
280 struct obd_device *obd = data;
281 struct obd_statfs osfs;
282 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
283 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
286 rc = seq_printf(m, "%u\n", osfs.os_bsize);
289 EXPORT_SYMBOL(lprocfs_rd_blksize);
291 int lprocfs_rd_kbytestotal(struct seq_file *m, void *data)
293 struct obd_device *obd = data;
294 struct obd_statfs osfs;
295 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
296 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
299 __u32 blk_size = osfs.os_bsize >> 10;
300 __u64 result = osfs.os_blocks;
302 while (blk_size >>= 1)
305 rc = seq_printf(m, LPU64"\n", result);
309 EXPORT_SYMBOL(lprocfs_rd_kbytestotal);
311 int lprocfs_rd_kbytesfree(struct seq_file *m, void *data)
313 struct obd_device *obd = data;
314 struct obd_statfs osfs;
315 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
316 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
319 __u32 blk_size = osfs.os_bsize >> 10;
320 __u64 result = osfs.os_bfree;
322 while (blk_size >>= 1)
325 rc = seq_printf(m, LPU64"\n", result);
329 EXPORT_SYMBOL(lprocfs_rd_kbytesfree);
331 int lprocfs_rd_kbytesavail(struct seq_file *m, void *data)
333 struct obd_device *obd = data;
334 struct obd_statfs osfs;
335 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
336 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
339 __u32 blk_size = osfs.os_bsize >> 10;
340 __u64 result = osfs.os_bavail;
342 while (blk_size >>= 1)
345 rc = seq_printf(m, LPU64"\n", result);
349 EXPORT_SYMBOL(lprocfs_rd_kbytesavail);
351 int lprocfs_rd_filestotal(struct seq_file *m, void *data)
353 struct obd_device *obd = data;
354 struct obd_statfs osfs;
355 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
356 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
359 rc = seq_printf(m, LPU64"\n", osfs.os_files);
363 EXPORT_SYMBOL(lprocfs_rd_filestotal);
365 int lprocfs_rd_filesfree(struct seq_file *m, void *data)
367 struct obd_device *obd = data;
368 struct obd_statfs osfs;
369 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
370 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
373 rc = seq_printf(m, LPU64"\n", osfs.os_ffree);
376 EXPORT_SYMBOL(lprocfs_rd_filesfree);
378 int lprocfs_rd_server_uuid(struct seq_file *m, void *data)
380 struct obd_device *obd = data;
381 struct obd_import *imp;
382 char *imp_state_name = NULL;
385 LASSERT(obd != NULL);
386 LPROCFS_CLIMP_CHECK(obd);
387 imp = obd->u.cli.cl_import;
388 imp_state_name = ptlrpc_import_state_name(imp->imp_state);
389 rc = seq_printf(m, "%s\t%s%s\n", obd2cli_tgt(obd), imp_state_name,
390 imp->imp_deactive ? "\tDEACTIVATED" : "");
392 LPROCFS_CLIMP_EXIT(obd);
395 EXPORT_SYMBOL(lprocfs_rd_server_uuid);
397 int lprocfs_rd_conn_uuid(struct seq_file *m, void *data)
399 struct obd_device *obd = data;
400 struct ptlrpc_connection *conn;
403 LASSERT(obd != NULL);
405 LPROCFS_CLIMP_CHECK(obd);
406 conn = obd->u.cli.cl_import->imp_connection;
407 if (conn && obd->u.cli.cl_import)
408 rc = seq_printf(m, "%s\n", conn->c_remote_uuid.uuid);
410 rc = seq_printf(m, "%s\n", "<none>");
412 LPROCFS_CLIMP_EXIT(obd);
415 EXPORT_SYMBOL(lprocfs_rd_conn_uuid);
417 /** add up per-cpu counters */
418 void lprocfs_stats_collect(struct lprocfs_stats *stats, int idx,
419 struct lprocfs_counter *cnt)
421 unsigned int num_entry;
422 struct lprocfs_counter *percpu_cntr;
423 struct lprocfs_counter_header *cntr_header;
425 unsigned long flags = 0;
427 memset(cnt, 0, sizeof(*cnt));
430 /* set count to 1 to avoid divide-by-zero errs in callers */
435 cnt->lc_min = LC_MIN_INIT;
437 num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
439 for (i = 0; i < num_entry; i++) {
440 if (stats->ls_percpu[i] == NULL)
442 cntr_header = &stats->ls_cnt_header[idx];
443 percpu_cntr = lprocfs_stats_counter_get(stats, i, idx);
445 cnt->lc_count += percpu_cntr->lc_count;
446 cnt->lc_sum += percpu_cntr->lc_sum;
447 if (percpu_cntr->lc_min < cnt->lc_min)
448 cnt->lc_min = percpu_cntr->lc_min;
449 if (percpu_cntr->lc_max > cnt->lc_max)
450 cnt->lc_max = percpu_cntr->lc_max;
451 cnt->lc_sumsquare += percpu_cntr->lc_sumsquare;
454 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
456 EXPORT_SYMBOL(lprocfs_stats_collect);
459 * Append a space separated list of current set flags to str.
461 #define flag2str(flag, first) \
463 if (imp->imp_##flag) \
464 seq_printf(m, "%s" #flag, first ? "" : ", "); \
466 static int obd_import_flags2str(struct obd_import *imp, struct seq_file *m)
470 if (imp->imp_obd->obd_no_recov) {
471 seq_printf(m, "no_recov");
475 flag2str(invalid, first);
477 flag2str(deactive, first);
478 flag2str(replayable, first);
479 flag2str(pingable, first);
484 static const char *obd_connect_names[] = {
498 "join_file(obsolete)",
502 "remote_client_by_force",
511 "mds_mds_connection",
514 "alt_checksum_algorithm",
540 static void obd_connect_seq_flags2str(struct seq_file *m, __u64 flags, char *sep)
546 for (i = 0; obd_connect_names[i] != NULL; i++, mask <<= 1) {
548 seq_printf(m, "%s%s",
549 first ? sep : "", obd_connect_names[i]);
553 if (flags & ~(mask - 1))
554 seq_printf(m, "%sunknown flags "LPX64,
555 first ? sep : "", flags & ~(mask - 1));
558 int obd_connect_flags2str(char *page, int count, __u64 flags, char *sep)
563 for (i = 0; obd_connect_names[i] != NULL; i++, mask <<= 1) {
565 ret += snprintf(page + ret, count - ret, "%s%s",
566 ret ? sep : "", obd_connect_names[i]);
568 if (flags & ~(mask - 1))
569 ret += snprintf(page + ret, count - ret,
570 "%sunknown flags "LPX64,
571 ret ? sep : "", flags & ~(mask - 1));
574 EXPORT_SYMBOL(obd_connect_flags2str);
576 int lprocfs_rd_import(struct seq_file *m, void *data)
578 struct lprocfs_counter ret;
579 struct lprocfs_counter_header *header;
580 struct obd_device *obd = (struct obd_device *)data;
581 struct obd_import *imp;
582 struct obd_import_conn *conn;
587 LASSERT(obd != NULL);
588 LPROCFS_CLIMP_CHECK(obd);
589 imp = obd->u.cli.cl_import;
600 ptlrpc_import_state_name(imp->imp_state),
601 imp->imp_connect_data.ocd_instance);
602 obd_connect_seq_flags2str(m, imp->imp_connect_data.ocd_connect_flags, ", ");
606 obd_import_flags2str(imp, m);
611 " failover_nids: [");
612 spin_lock(&imp->imp_lock);
614 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
615 seq_printf(m, "%s%s", j ? ", " : "",
616 libcfs_nid2str(conn->oic_conn->c_peer.nid));
621 " current_connection: %s\n"
622 " connection_attempts: %u\n"
624 " in-progress_invalidations: %u\n",
625 imp->imp_connection == NULL ? "<none>" :
626 libcfs_nid2str(imp->imp_connection->c_peer.nid),
629 atomic_read(&imp->imp_inval_count));
630 spin_unlock(&imp->imp_lock);
632 if (obd->obd_svc_stats == NULL)
635 header = &obd->obd_svc_stats->ls_cnt_header[PTLRPC_REQWAIT_CNTR];
636 lprocfs_stats_collect(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR, &ret);
637 if (ret.lc_count != 0) {
638 /* first argument to do_div MUST be __u64 */
639 __u64 sum = ret.lc_sum;
640 do_div(sum, ret.lc_count);
647 " unregistering: %u\n"
649 " avg_waittime: "LPU64" %s\n",
650 atomic_read(&imp->imp_inflight),
651 atomic_read(&imp->imp_unregistering),
652 atomic_read(&imp->imp_timeouts),
653 ret.lc_sum, header->lc_units);
656 for(j = 0; j < IMP_AT_MAX_PORTALS; j++) {
657 if (imp->imp_at.iat_portal[j] == 0)
659 k = max_t(unsigned int, k,
660 at_get(&imp->imp_at.iat_service_estimate[j]));
663 " service_estimates:\n"
664 " services: %u sec\n"
665 " network: %u sec\n",
667 at_get(&imp->imp_at.iat_net_latency));
671 " last_replay: "LPU64"\n"
672 " peer_committed: "LPU64"\n"
673 " last_checked: "LPU64"\n",
674 imp->imp_last_replay_transno,
675 imp->imp_peer_committed_transno,
676 imp->imp_last_transno_checked);
679 for (rw = 0; rw <= 1; rw++) {
680 lprocfs_stats_collect(obd->obd_svc_stats,
681 PTLRPC_LAST_CNTR + BRW_READ_BYTES + rw,
683 if (ret.lc_sum > 0 && ret.lc_count > 0) {
684 /* first argument to do_div MUST be __u64 */
685 __u64 sum = ret.lc_sum;
686 do_div(sum, ret.lc_count);
689 " %s_data_averages:\n"
690 " bytes_per_rpc: "LPU64"\n",
691 rw ? "write" : "read",
695 j = opcode_offset(OST_READ + rw) + EXTRA_MAX_OPCODES;
696 header = &obd->obd_svc_stats->ls_cnt_header[j];
697 lprocfs_stats_collect(obd->obd_svc_stats, j, &ret);
698 if (ret.lc_sum > 0 && ret.lc_count != 0) {
699 /* first argument to do_div MUST be __u64 */
700 __u64 sum = ret.lc_sum;
701 do_div(sum, ret.lc_count);
704 " %s_per_rpc: "LPU64"\n",
705 header->lc_units, ret.lc_sum);
709 " MB_per_sec: %u.%.02u\n",
710 k / j, (100 * k / j) % 100);
715 LPROCFS_CLIMP_EXIT(obd);
718 EXPORT_SYMBOL(lprocfs_rd_import);
720 int lprocfs_rd_state(struct seq_file *m, void *data)
722 struct obd_device *obd = (struct obd_device *)data;
723 struct obd_import *imp;
726 LASSERT(obd != NULL);
727 LPROCFS_CLIMP_CHECK(obd);
728 imp = obd->u.cli.cl_import;
730 seq_printf(m, "current_state: %s\n",
731 ptlrpc_import_state_name(imp->imp_state));
732 seq_printf(m, "state_history:\n");
733 k = imp->imp_state_hist_idx;
734 for (j = 0; j < IMP_STATE_HIST_LEN; j++) {
735 struct import_state_hist *ish =
736 &imp->imp_state_hist[(k + j) % IMP_STATE_HIST_LEN];
737 if (ish->ish_state == 0)
739 seq_printf(m, " - ["CFS_TIME_T", %s]\n",
741 ptlrpc_import_state_name(ish->ish_state));
744 LPROCFS_CLIMP_EXIT(obd);
747 EXPORT_SYMBOL(lprocfs_rd_state);
749 int lprocfs_at_hist_helper(struct seq_file *m, struct adaptive_timeout *at)
752 for (i = 0; i < AT_BINS; i++)
753 seq_printf(m, "%3u ", at->at_hist[i]);
757 EXPORT_SYMBOL(lprocfs_at_hist_helper);
759 /* See also ptlrpc_lprocfs_rd_timeouts */
760 int lprocfs_rd_timeouts(struct seq_file *m, void *data)
762 struct obd_device *obd = (struct obd_device *)data;
763 struct obd_import *imp;
764 unsigned int cur, worst;
769 LASSERT(obd != NULL);
770 LPROCFS_CLIMP_CHECK(obd);
771 imp = obd->u.cli.cl_import;
773 now = cfs_time_current_sec();
775 /* Some network health info for kicks */
776 s2dhms(&ts, now - imp->imp_last_reply_time);
777 seq_printf(m, "%-10s : %ld, "DHMS_FMT" ago\n",
778 "last reply", imp->imp_last_reply_time, DHMS_VARS(&ts));
780 cur = at_get(&imp->imp_at.iat_net_latency);
781 worst = imp->imp_at.iat_net_latency.at_worst_ever;
782 worstt = imp->imp_at.iat_net_latency.at_worst_time;
783 s2dhms(&ts, now - worstt);
784 seq_printf(m, "%-10s : cur %3u worst %3u (at %ld, "DHMS_FMT" ago) ",
785 "network", cur, worst, worstt, DHMS_VARS(&ts));
786 lprocfs_at_hist_helper(m, &imp->imp_at.iat_net_latency);
788 for(i = 0; i < IMP_AT_MAX_PORTALS; i++) {
789 if (imp->imp_at.iat_portal[i] == 0)
791 cur = at_get(&imp->imp_at.iat_service_estimate[i]);
792 worst = imp->imp_at.iat_service_estimate[i].at_worst_ever;
793 worstt = imp->imp_at.iat_service_estimate[i].at_worst_time;
794 s2dhms(&ts, now - worstt);
795 seq_printf(m, "portal %-2d : cur %3u worst %3u (at %ld, "
796 DHMS_FMT" ago) ", imp->imp_at.iat_portal[i],
797 cur, worst, worstt, DHMS_VARS(&ts));
798 lprocfs_at_hist_helper(m, &imp->imp_at.iat_service_estimate[i]);
801 LPROCFS_CLIMP_EXIT(obd);
804 EXPORT_SYMBOL(lprocfs_rd_timeouts);
806 int lprocfs_rd_connect_flags(struct seq_file *m, void *data)
808 struct obd_device *obd = data;
811 LPROCFS_CLIMP_CHECK(obd);
812 flags = obd->u.cli.cl_import->imp_connect_data.ocd_connect_flags;
813 seq_printf(m, "flags="LPX64"\n", flags);
814 obd_connect_seq_flags2str(m, flags, "\n");
816 LPROCFS_CLIMP_EXIT(obd);
819 EXPORT_SYMBOL(lprocfs_rd_connect_flags);
821 int lprocfs_rd_num_exports(struct seq_file *m, void *data)
823 struct obd_device *obd = data;
825 LASSERT(obd != NULL);
826 return seq_printf(m, "%u\n", obd->obd_num_exports);
828 EXPORT_SYMBOL(lprocfs_rd_num_exports);
830 int lprocfs_rd_numrefs(struct seq_file *m, void *data)
832 struct obd_type *class = (struct obd_type*) data;
834 LASSERT(class != NULL);
835 return seq_printf(m, "%d\n", class->typ_refcnt);
837 EXPORT_SYMBOL(lprocfs_rd_numrefs);
839 int lprocfs_obd_setup(struct obd_device *obd, struct lprocfs_vars *list)
843 LASSERT(obd != NULL);
844 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
845 LASSERT(obd->obd_type->typ_procroot != NULL);
847 obd->obd_proc_entry = lprocfs_register(obd->obd_name,
848 obd->obd_type->typ_procroot,
850 if (IS_ERR(obd->obd_proc_entry)) {
851 rc = PTR_ERR(obd->obd_proc_entry);
852 CERROR("error %d setting up lprocfs for %s\n",rc,obd->obd_name);
853 obd->obd_proc_entry = NULL;
857 EXPORT_SYMBOL(lprocfs_obd_setup);
859 int lprocfs_obd_cleanup(struct obd_device *obd)
863 if (obd->obd_proc_exports_entry) {
864 /* Should be no exports left */
865 lprocfs_remove(&obd->obd_proc_exports_entry);
866 obd->obd_proc_exports_entry = NULL;
868 if (obd->obd_proc_entry) {
869 lprocfs_remove(&obd->obd_proc_entry);
870 obd->obd_proc_entry = NULL;
874 EXPORT_SYMBOL(lprocfs_obd_cleanup);
876 static void lprocfs_free_client_stats(struct nid_stat *client_stat)
878 CDEBUG(D_CONFIG, "stat %p - data %p/%p\n", client_stat,
879 client_stat->nid_proc, client_stat->nid_stats);
881 LASSERTF(atomic_read(&client_stat->nid_exp_ref_count) == 0,
882 "nid %s:count %d\n", libcfs_nid2str(client_stat->nid),
883 atomic_read(&client_stat->nid_exp_ref_count));
885 if (client_stat->nid_proc)
886 lprocfs_remove(&client_stat->nid_proc);
888 if (client_stat->nid_stats)
889 lprocfs_free_stats(&client_stat->nid_stats);
891 if (client_stat->nid_ldlm_stats)
892 lprocfs_free_stats(&client_stat->nid_ldlm_stats);
894 OBD_FREE_PTR(client_stat);
899 void lprocfs_free_per_client_stats(struct obd_device *obd)
901 struct cfs_hash *hash = obd->obd_nid_stats_hash;
902 struct nid_stat *stat;
904 /* we need extra list - because hash_exit called to early */
905 /* not need locking because all clients is died */
906 while (!list_empty(&obd->obd_nid_stats)) {
907 stat = list_entry(obd->obd_nid_stats.next,
908 struct nid_stat, nid_list);
909 list_del_init(&stat->nid_list);
910 cfs_hash_del(hash, &stat->nid, &stat->nid_hash);
911 lprocfs_free_client_stats(stat);
914 EXPORT_SYMBOL(lprocfs_free_per_client_stats);
916 struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num,
917 enum lprocfs_stats_flags flags)
919 struct lprocfs_stats *stats;
920 unsigned int num_entry;
921 unsigned int percpusize = 0;
927 if (lprocfs_no_percpu_stats != 0)
928 flags |= LPROCFS_STATS_FLAG_NOPERCPU;
930 if (flags & LPROCFS_STATS_FLAG_NOPERCPU)
933 num_entry = num_possible_cpus();
935 /* alloc percpu pointers for all possible cpu slots */
936 LIBCFS_ALLOC(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
941 stats->ls_flags = flags;
942 spin_lock_init(&stats->ls_lock);
944 /* alloc num of counter headers */
945 LIBCFS_ALLOC(stats->ls_cnt_header,
946 stats->ls_num * sizeof(struct lprocfs_counter_header));
947 if (stats->ls_cnt_header == NULL)
950 if ((flags & LPROCFS_STATS_FLAG_NOPERCPU) != 0) {
951 /* contains only one set counters */
952 percpusize = lprocfs_stats_counter_size(stats);
953 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[0], percpusize);
954 if (stats->ls_percpu[0] == NULL)
956 stats->ls_biggest_alloc_num = 1;
957 } else if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0) {
958 /* alloc all percpu data, currently only obd_memory use this */
959 for (i = 0; i < num_entry; ++i)
960 if (lprocfs_stats_alloc_one(stats, i) < 0)
967 lprocfs_free_stats(&stats);
970 EXPORT_SYMBOL(lprocfs_alloc_stats);
972 void lprocfs_free_stats(struct lprocfs_stats **statsh)
974 struct lprocfs_stats *stats = *statsh;
975 unsigned int num_entry;
976 unsigned int percpusize;
979 if (stats == NULL || stats->ls_num == 0)
983 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU)
986 num_entry = num_possible_cpus();
988 percpusize = lprocfs_stats_counter_size(stats);
989 for (i = 0; i < num_entry; i++)
990 if (stats->ls_percpu[i] != NULL)
991 LIBCFS_FREE(stats->ls_percpu[i], percpusize);
992 if (stats->ls_cnt_header != NULL)
993 LIBCFS_FREE(stats->ls_cnt_header, stats->ls_num *
994 sizeof(struct lprocfs_counter_header));
995 LIBCFS_FREE(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
997 EXPORT_SYMBOL(lprocfs_free_stats);
999 void lprocfs_clear_stats(struct lprocfs_stats *stats)
1001 struct lprocfs_counter *percpu_cntr;
1002 struct lprocfs_counter_header *header;
1005 unsigned int num_entry;
1006 unsigned long flags = 0;
1008 num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1010 for (i = 0; i < num_entry; i++) {
1011 if (stats->ls_percpu[i] == NULL)
1013 for (j = 0; j < stats->ls_num; j++) {
1014 header = &stats->ls_cnt_header[j];
1015 percpu_cntr = lprocfs_stats_counter_get(stats, i, j);
1016 percpu_cntr->lc_count = 0;
1017 percpu_cntr->lc_min = LC_MIN_INIT;
1018 percpu_cntr->lc_max = 0;
1019 percpu_cntr->lc_sumsquare = 0;
1020 percpu_cntr->lc_sum = 0;
1021 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1022 percpu_cntr->lc_sum_irq = 0;
1026 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1028 EXPORT_SYMBOL(lprocfs_clear_stats);
1030 static ssize_t lprocfs_stats_seq_write(struct file *file,
1031 const char __user *buf,
1032 size_t len, loff_t *off)
1034 struct seq_file *seq = file->private_data;
1035 struct lprocfs_stats *stats = seq->private;
1037 lprocfs_clear_stats(stats);
1042 static void *lprocfs_stats_seq_start(struct seq_file *p, loff_t *pos)
1044 struct lprocfs_stats *stats = p->private;
1046 return (*pos < stats->ls_num) ? pos : NULL;
1049 static void lprocfs_stats_seq_stop(struct seq_file *p, void *v)
1053 static void *lprocfs_stats_seq_next(struct seq_file *p, void *v, loff_t *pos)
1056 return lprocfs_stats_seq_start(p, pos);
1059 /* seq file export of one lprocfs counter */
1060 static int lprocfs_stats_seq_show(struct seq_file *p, void *v)
1062 struct lprocfs_stats *stats = p->private;
1063 struct lprocfs_counter_header *hdr;
1064 struct lprocfs_counter ctr;
1065 int idx = *(loff_t *)v;
1070 do_gettimeofday(&now);
1071 rc = seq_printf(p, "%-25s %lu.%lu secs.usecs\n",
1072 "snapshot_time", now.tv_sec, (unsigned long)now.tv_usec);
1076 hdr = &stats->ls_cnt_header[idx];
1077 lprocfs_stats_collect(stats, idx, &ctr);
1079 if (ctr.lc_count == 0)
1082 rc = seq_printf(p, "%-25s "LPD64" samples [%s]", hdr->lc_name,
1083 ctr.lc_count, hdr->lc_units);
1088 if ((hdr->lc_config & LPROCFS_CNTR_AVGMINMAX) && (ctr.lc_count > 0)) {
1089 rc = seq_printf(p, " "LPD64" "LPD64" "LPD64,
1090 ctr.lc_min, ctr.lc_max, ctr.lc_sum);
1093 if (hdr->lc_config & LPROCFS_CNTR_STDDEV)
1094 rc = seq_printf(p, " "LPD64, ctr.lc_sumsquare);
1098 rc = seq_printf(p, "\n");
1100 return (rc < 0) ? rc : 0;
1103 struct seq_operations lprocfs_stats_seq_sops = {
1104 .start = lprocfs_stats_seq_start,
1105 .stop = lprocfs_stats_seq_stop,
1106 .next = lprocfs_stats_seq_next,
1107 .show = lprocfs_stats_seq_show,
1110 static int lprocfs_stats_seq_open(struct inode *inode, struct file *file)
1112 struct seq_file *seq;
1115 rc = seq_open(file, &lprocfs_stats_seq_sops);
1118 seq = file->private_data;
1119 seq->private = PDE_DATA(inode);
1123 struct file_operations lprocfs_stats_seq_fops = {
1124 .owner = THIS_MODULE,
1125 .open = lprocfs_stats_seq_open,
1127 .write = lprocfs_stats_seq_write,
1128 .llseek = seq_lseek,
1129 .release = lprocfs_seq_release,
1132 int lprocfs_register_stats(struct proc_dir_entry *root, const char *name,
1133 struct lprocfs_stats *stats)
1135 struct proc_dir_entry *entry;
1136 LASSERT(root != NULL);
1138 entry = proc_create_data(name, 0644, root,
1139 &lprocfs_stats_seq_fops, stats);
1145 EXPORT_SYMBOL(lprocfs_register_stats);
1147 void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
1148 unsigned conf, const char *name, const char *units)
1150 struct lprocfs_counter_header *header;
1151 struct lprocfs_counter *percpu_cntr;
1152 unsigned long flags = 0;
1154 unsigned int num_cpu;
1156 LASSERT(stats != NULL);
1158 header = &stats->ls_cnt_header[index];
1159 LASSERTF(header != NULL, "Failed to allocate stats header:[%d]%s/%s\n",
1160 index, name, units);
1162 header->lc_config = conf;
1163 header->lc_name = name;
1164 header->lc_units = units;
1166 num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1167 for (i = 0; i < num_cpu; ++i) {
1168 if (stats->ls_percpu[i] == NULL)
1170 percpu_cntr = lprocfs_stats_counter_get(stats, i, index);
1171 percpu_cntr->lc_count = 0;
1172 percpu_cntr->lc_min = LC_MIN_INIT;
1173 percpu_cntr->lc_max = 0;
1174 percpu_cntr->lc_sumsquare = 0;
1175 percpu_cntr->lc_sum = 0;
1176 if ((stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1177 percpu_cntr->lc_sum_irq = 0;
1179 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1181 EXPORT_SYMBOL(lprocfs_counter_init);
1183 #define LPROCFS_OBD_OP_INIT(base, stats, op) \
1185 unsigned int coffset = base + OBD_COUNTER_OFFSET(op); \
1186 LASSERT(coffset < stats->ls_num); \
1187 lprocfs_counter_init(stats, coffset, 0, #op, "reqs"); \
1190 void lprocfs_init_ops_stats(int num_private_stats, struct lprocfs_stats *stats)
1192 LPROCFS_OBD_OP_INIT(num_private_stats, stats, iocontrol);
1193 LPROCFS_OBD_OP_INIT(num_private_stats, stats, get_info);
1194 LPROCFS_OBD_OP_INIT(num_private_stats, stats, set_info_async);
1195 LPROCFS_OBD_OP_INIT(num_private_stats, stats, attach);
1196 LPROCFS_OBD_OP_INIT(num_private_stats, stats, detach);
1197 LPROCFS_OBD_OP_INIT(num_private_stats, stats, setup);
1198 LPROCFS_OBD_OP_INIT(num_private_stats, stats, precleanup);
1199 LPROCFS_OBD_OP_INIT(num_private_stats, stats, cleanup);
1200 LPROCFS_OBD_OP_INIT(num_private_stats, stats, process_config);
1201 LPROCFS_OBD_OP_INIT(num_private_stats, stats, postrecov);
1202 LPROCFS_OBD_OP_INIT(num_private_stats, stats, add_conn);
1203 LPROCFS_OBD_OP_INIT(num_private_stats, stats, del_conn);
1204 LPROCFS_OBD_OP_INIT(num_private_stats, stats, connect);
1205 LPROCFS_OBD_OP_INIT(num_private_stats, stats, reconnect);
1206 LPROCFS_OBD_OP_INIT(num_private_stats, stats, disconnect);
1207 LPROCFS_OBD_OP_INIT(num_private_stats, stats, fid_init);
1208 LPROCFS_OBD_OP_INIT(num_private_stats, stats, fid_fini);
1209 LPROCFS_OBD_OP_INIT(num_private_stats, stats, fid_alloc);
1210 LPROCFS_OBD_OP_INIT(num_private_stats, stats, statfs);
1211 LPROCFS_OBD_OP_INIT(num_private_stats, stats, statfs_async);
1212 LPROCFS_OBD_OP_INIT(num_private_stats, stats, packmd);
1213 LPROCFS_OBD_OP_INIT(num_private_stats, stats, unpackmd);
1214 LPROCFS_OBD_OP_INIT(num_private_stats, stats, preallocate);
1215 LPROCFS_OBD_OP_INIT(num_private_stats, stats, precreate);
1216 LPROCFS_OBD_OP_INIT(num_private_stats, stats, create);
1217 LPROCFS_OBD_OP_INIT(num_private_stats, stats, create_async);
1218 LPROCFS_OBD_OP_INIT(num_private_stats, stats, destroy);
1219 LPROCFS_OBD_OP_INIT(num_private_stats, stats, setattr);
1220 LPROCFS_OBD_OP_INIT(num_private_stats, stats, setattr_async);
1221 LPROCFS_OBD_OP_INIT(num_private_stats, stats, getattr);
1222 LPROCFS_OBD_OP_INIT(num_private_stats, stats, getattr_async);
1223 LPROCFS_OBD_OP_INIT(num_private_stats, stats, brw);
1224 LPROCFS_OBD_OP_INIT(num_private_stats, stats, merge_lvb);
1225 LPROCFS_OBD_OP_INIT(num_private_stats, stats, adjust_kms);
1226 LPROCFS_OBD_OP_INIT(num_private_stats, stats, punch);
1227 LPROCFS_OBD_OP_INIT(num_private_stats, stats, sync);
1228 LPROCFS_OBD_OP_INIT(num_private_stats, stats, migrate);
1229 LPROCFS_OBD_OP_INIT(num_private_stats, stats, copy);
1230 LPROCFS_OBD_OP_INIT(num_private_stats, stats, iterate);
1231 LPROCFS_OBD_OP_INIT(num_private_stats, stats, preprw);
1232 LPROCFS_OBD_OP_INIT(num_private_stats, stats, commitrw);
1233 LPROCFS_OBD_OP_INIT(num_private_stats, stats, enqueue);
1234 LPROCFS_OBD_OP_INIT(num_private_stats, stats, change_cbdata);
1235 LPROCFS_OBD_OP_INIT(num_private_stats, stats, find_cbdata);
1236 LPROCFS_OBD_OP_INIT(num_private_stats, stats, cancel);
1237 LPROCFS_OBD_OP_INIT(num_private_stats, stats, cancel_unused);
1238 LPROCFS_OBD_OP_INIT(num_private_stats, stats, init_export);
1239 LPROCFS_OBD_OP_INIT(num_private_stats, stats, destroy_export);
1240 LPROCFS_OBD_OP_INIT(num_private_stats, stats, extent_calc);
1241 LPROCFS_OBD_OP_INIT(num_private_stats, stats, llog_init);
1242 LPROCFS_OBD_OP_INIT(num_private_stats, stats, llog_connect);
1243 LPROCFS_OBD_OP_INIT(num_private_stats, stats, llog_finish);
1244 LPROCFS_OBD_OP_INIT(num_private_stats, stats, pin);
1245 LPROCFS_OBD_OP_INIT(num_private_stats, stats, unpin);
1246 LPROCFS_OBD_OP_INIT(num_private_stats, stats, import_event);
1247 LPROCFS_OBD_OP_INIT(num_private_stats, stats, notify);
1248 LPROCFS_OBD_OP_INIT(num_private_stats, stats, health_check);
1249 LPROCFS_OBD_OP_INIT(num_private_stats, stats, get_uuid);
1250 LPROCFS_OBD_OP_INIT(num_private_stats, stats, quotacheck);
1251 LPROCFS_OBD_OP_INIT(num_private_stats, stats, quotactl);
1252 LPROCFS_OBD_OP_INIT(num_private_stats, stats, ping);
1253 LPROCFS_OBD_OP_INIT(num_private_stats, stats, pool_new);
1254 LPROCFS_OBD_OP_INIT(num_private_stats, stats, pool_rem);
1255 LPROCFS_OBD_OP_INIT(num_private_stats, stats, pool_add);
1256 LPROCFS_OBD_OP_INIT(num_private_stats, stats, pool_del);
1257 LPROCFS_OBD_OP_INIT(num_private_stats, stats, getref);
1258 LPROCFS_OBD_OP_INIT(num_private_stats, stats, putref);
1260 EXPORT_SYMBOL(lprocfs_init_ops_stats);
1262 int lprocfs_alloc_obd_stats(struct obd_device *obd, unsigned num_private_stats)
1264 struct lprocfs_stats *stats;
1265 unsigned int num_stats;
1268 LASSERT(obd->obd_stats == NULL);
1269 LASSERT(obd->obd_proc_entry != NULL);
1270 LASSERT(obd->obd_cntr_base == 0);
1272 num_stats = ((int)sizeof(*obd->obd_type->typ_dt_ops) / sizeof(void *)) +
1273 num_private_stats - 1 /* o_owner */;
1274 stats = lprocfs_alloc_stats(num_stats, 0);
1278 lprocfs_init_ops_stats(num_private_stats, stats);
1280 for (i = num_private_stats; i < num_stats; i++) {
1281 /* If this LBUGs, it is likely that an obd
1282 * operation was added to struct obd_ops in
1283 * <obd.h>, and that the corresponding line item
1284 * LPROCFS_OBD_OP_INIT(.., .., opname)
1285 * is missing from the list above. */
1286 LASSERTF(stats->ls_cnt_header[i].lc_name != NULL,
1287 "Missing obd_stat initializer obd_op "
1288 "operation at offset %d.\n", i - num_private_stats);
1290 rc = lprocfs_register_stats(obd->obd_proc_entry, "stats", stats);
1292 lprocfs_free_stats(&stats);
1294 obd->obd_stats = stats;
1295 obd->obd_cntr_base = num_private_stats;
1299 EXPORT_SYMBOL(lprocfs_alloc_obd_stats);
1301 void lprocfs_free_obd_stats(struct obd_device *obd)
1304 lprocfs_free_stats(&obd->obd_stats);
1306 EXPORT_SYMBOL(lprocfs_free_obd_stats);
1308 #define LPROCFS_MD_OP_INIT(base, stats, op) \
1310 unsigned int coffset = base + MD_COUNTER_OFFSET(op); \
1311 LASSERT(coffset < stats->ls_num); \
1312 lprocfs_counter_init(stats, coffset, 0, #op, "reqs"); \
1315 void lprocfs_init_mps_stats(int num_private_stats, struct lprocfs_stats *stats)
1317 LPROCFS_MD_OP_INIT(num_private_stats, stats, getstatus);
1318 LPROCFS_MD_OP_INIT(num_private_stats, stats, null_inode);
1319 LPROCFS_MD_OP_INIT(num_private_stats, stats, find_cbdata);
1320 LPROCFS_MD_OP_INIT(num_private_stats, stats, close);
1321 LPROCFS_MD_OP_INIT(num_private_stats, stats, create);
1322 LPROCFS_MD_OP_INIT(num_private_stats, stats, done_writing);
1323 LPROCFS_MD_OP_INIT(num_private_stats, stats, enqueue);
1324 LPROCFS_MD_OP_INIT(num_private_stats, stats, getattr);
1325 LPROCFS_MD_OP_INIT(num_private_stats, stats, getattr_name);
1326 LPROCFS_MD_OP_INIT(num_private_stats, stats, intent_lock);
1327 LPROCFS_MD_OP_INIT(num_private_stats, stats, link);
1328 LPROCFS_MD_OP_INIT(num_private_stats, stats, rename);
1329 LPROCFS_MD_OP_INIT(num_private_stats, stats, is_subdir);
1330 LPROCFS_MD_OP_INIT(num_private_stats, stats, setattr);
1331 LPROCFS_MD_OP_INIT(num_private_stats, stats, sync);
1332 LPROCFS_MD_OP_INIT(num_private_stats, stats, readpage);
1333 LPROCFS_MD_OP_INIT(num_private_stats, stats, unlink);
1334 LPROCFS_MD_OP_INIT(num_private_stats, stats, setxattr);
1335 LPROCFS_MD_OP_INIT(num_private_stats, stats, getxattr);
1336 LPROCFS_MD_OP_INIT(num_private_stats, stats, init_ea_size);
1337 LPROCFS_MD_OP_INIT(num_private_stats, stats, get_lustre_md);
1338 LPROCFS_MD_OP_INIT(num_private_stats, stats, free_lustre_md);
1339 LPROCFS_MD_OP_INIT(num_private_stats, stats, set_open_replay_data);
1340 LPROCFS_MD_OP_INIT(num_private_stats, stats, clear_open_replay_data);
1341 LPROCFS_MD_OP_INIT(num_private_stats, stats, set_lock_data);
1342 LPROCFS_MD_OP_INIT(num_private_stats, stats, lock_match);
1343 LPROCFS_MD_OP_INIT(num_private_stats, stats, cancel_unused);
1344 LPROCFS_MD_OP_INIT(num_private_stats, stats, renew_capa);
1345 LPROCFS_MD_OP_INIT(num_private_stats, stats, unpack_capa);
1346 LPROCFS_MD_OP_INIT(num_private_stats, stats, get_remote_perm);
1347 LPROCFS_MD_OP_INIT(num_private_stats, stats, intent_getattr_async);
1348 LPROCFS_MD_OP_INIT(num_private_stats, stats, revalidate_lock);
1350 EXPORT_SYMBOL(lprocfs_init_mps_stats);
1352 int lprocfs_alloc_md_stats(struct obd_device *obd,
1353 unsigned num_private_stats)
1355 struct lprocfs_stats *stats;
1356 unsigned int num_stats;
1359 LASSERT(obd->md_stats == NULL);
1360 LASSERT(obd->obd_proc_entry != NULL);
1361 LASSERT(obd->md_cntr_base == 0);
1363 num_stats = 1 + MD_COUNTER_OFFSET(revalidate_lock) +
1365 stats = lprocfs_alloc_stats(num_stats, 0);
1369 lprocfs_init_mps_stats(num_private_stats, stats);
1371 for (i = num_private_stats; i < num_stats; i++) {
1372 if (stats->ls_cnt_header[i].lc_name == NULL) {
1373 CERROR("Missing md_stat initializer md_op "
1374 "operation at offset %d. Aborting.\n",
1375 i - num_private_stats);
1379 rc = lprocfs_register_stats(obd->obd_proc_entry, "md_stats", stats);
1381 lprocfs_free_stats(&stats);
1383 obd->md_stats = stats;
1384 obd->md_cntr_base = num_private_stats;
1388 EXPORT_SYMBOL(lprocfs_alloc_md_stats);
1390 void lprocfs_free_md_stats(struct obd_device *obd)
1392 struct lprocfs_stats *stats = obd->md_stats;
1394 if (stats != NULL) {
1395 obd->md_stats = NULL;
1396 obd->md_cntr_base = 0;
1397 lprocfs_free_stats(&stats);
1400 EXPORT_SYMBOL(lprocfs_free_md_stats);
1402 void lprocfs_init_ldlm_stats(struct lprocfs_stats *ldlm_stats)
1404 lprocfs_counter_init(ldlm_stats,
1405 LDLM_ENQUEUE - LDLM_FIRST_OPC,
1406 0, "ldlm_enqueue", "reqs");
1407 lprocfs_counter_init(ldlm_stats,
1408 LDLM_CONVERT - LDLM_FIRST_OPC,
1409 0, "ldlm_convert", "reqs");
1410 lprocfs_counter_init(ldlm_stats,
1411 LDLM_CANCEL - LDLM_FIRST_OPC,
1412 0, "ldlm_cancel", "reqs");
1413 lprocfs_counter_init(ldlm_stats,
1414 LDLM_BL_CALLBACK - LDLM_FIRST_OPC,
1415 0, "ldlm_bl_callback", "reqs");
1416 lprocfs_counter_init(ldlm_stats,
1417 LDLM_CP_CALLBACK - LDLM_FIRST_OPC,
1418 0, "ldlm_cp_callback", "reqs");
1419 lprocfs_counter_init(ldlm_stats,
1420 LDLM_GL_CALLBACK - LDLM_FIRST_OPC,
1421 0, "ldlm_gl_callback", "reqs");
1423 EXPORT_SYMBOL(lprocfs_init_ldlm_stats);
1425 int lprocfs_exp_print_uuid(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1426 struct hlist_node *hnode, void *data)
1429 struct obd_export *exp = cfs_hash_object(hs, hnode);
1430 struct seq_file *m = (struct seq_file *)data;
1432 if (exp->exp_nid_stats)
1433 seq_printf(m, "%s\n", obd_uuid2str(&exp->exp_client_uuid));
1439 lproc_exp_uuid_seq_show(struct seq_file *m, void *unused)
1441 struct nid_stat *stats = (struct nid_stat *)m->private;
1442 struct obd_device *obd = stats->nid_obd;
1444 cfs_hash_for_each_key(obd->obd_nid_hash, &stats->nid,
1445 lprocfs_exp_print_uuid, m);
1449 LPROC_SEQ_FOPS_RO(lproc_exp_uuid);
1451 struct exp_hash_cb_data {
1456 int lprocfs_exp_print_hash(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1457 struct hlist_node *hnode, void *cb_data)
1460 struct exp_hash_cb_data *data = (struct exp_hash_cb_data *)cb_data;
1461 struct obd_export *exp = cfs_hash_object(hs, hnode);
1463 if (exp->exp_lock_hash != NULL) {
1465 cfs_hash_debug_header(data->m);
1466 data->first = false;
1468 cfs_hash_debug_str(hs, data->m);
1475 lproc_exp_hash_seq_show(struct seq_file *m, void *unused)
1477 struct nid_stat *stats = (struct nid_stat *)m->private;
1478 struct obd_device *obd = stats->nid_obd;
1479 struct exp_hash_cb_data cb_data = {m, true};
1481 cfs_hash_for_each_key(obd->obd_nid_hash, &stats->nid,
1482 lprocfs_exp_print_hash, &cb_data);
1486 LPROC_SEQ_FOPS_RO(lproc_exp_hash);
1488 int lprocfs_nid_stats_clear_read(struct seq_file *m, void *data)
1490 return seq_printf(m, "%s\n",
1491 "Write into this file to clear all nid stats and "
1492 "stale nid entries");
1494 EXPORT_SYMBOL(lprocfs_nid_stats_clear_read);
1496 static int lprocfs_nid_stats_clear_write_cb(void *obj, void *data)
1498 struct nid_stat *stat = obj;
1500 CDEBUG(D_INFO,"refcnt %d\n", atomic_read(&stat->nid_exp_ref_count));
1501 if (atomic_read(&stat->nid_exp_ref_count) == 1) {
1502 /* object has only hash references. */
1503 spin_lock(&stat->nid_obd->obd_nid_lock);
1504 list_move(&stat->nid_list, data);
1505 spin_unlock(&stat->nid_obd->obd_nid_lock);
1508 /* we has reference to object - only clear data*/
1509 if (stat->nid_stats)
1510 lprocfs_clear_stats(stat->nid_stats);
1515 int lprocfs_nid_stats_clear_write(struct file *file, const char *buffer,
1516 unsigned long count, void *data)
1518 struct obd_device *obd = (struct obd_device *)data;
1519 struct nid_stat *client_stat;
1520 LIST_HEAD(free_list);
1522 cfs_hash_cond_del(obd->obd_nid_stats_hash,
1523 lprocfs_nid_stats_clear_write_cb, &free_list);
1525 while (!list_empty(&free_list)) {
1526 client_stat = list_entry(free_list.next, struct nid_stat,
1528 list_del_init(&client_stat->nid_list);
1529 lprocfs_free_client_stats(client_stat);
1534 EXPORT_SYMBOL(lprocfs_nid_stats_clear_write);
1536 int lprocfs_exp_setup(struct obd_export *exp, lnet_nid_t *nid, int *newnid)
1538 struct nid_stat *new_stat, *old_stat;
1539 struct obd_device *obd = NULL;
1540 struct proc_dir_entry *entry;
1541 char *buffer = NULL;
1546 if (!exp || !exp->exp_obd || !exp->exp_obd->obd_proc_exports_entry ||
1547 !exp->exp_obd->obd_nid_stats_hash)
1550 /* not test against zero because eric say:
1551 * You may only test nid against another nid, or LNET_NID_ANY.
1552 * Anything else is nonsense.*/
1553 if (!nid || *nid == LNET_NID_ANY)
1558 CDEBUG(D_CONFIG, "using hash %p\n", obd->obd_nid_stats_hash);
1560 OBD_ALLOC_PTR(new_stat);
1561 if (new_stat == NULL)
1564 new_stat->nid = *nid;
1565 new_stat->nid_obd = exp->exp_obd;
1566 /* we need set default refcount to 1 to balance obd_disconnect */
1567 atomic_set(&new_stat->nid_exp_ref_count, 1);
1569 old_stat = cfs_hash_findadd_unique(obd->obd_nid_stats_hash,
1570 nid, &new_stat->nid_hash);
1571 CDEBUG(D_INFO, "Found stats %p for nid %s - ref %d\n",
1572 old_stat, libcfs_nid2str(*nid),
1573 atomic_read(&new_stat->nid_exp_ref_count));
1575 /* We need to release old stats because lprocfs_exp_cleanup() hasn't
1576 * been and will never be called. */
1577 if (exp->exp_nid_stats) {
1578 nidstat_putref(exp->exp_nid_stats);
1579 exp->exp_nid_stats = NULL;
1582 /* Return -EALREADY here so that we know that the /proc
1583 * entry already has been created */
1584 if (old_stat != new_stat) {
1585 exp->exp_nid_stats = old_stat;
1586 GOTO(destroy_new, rc = -EALREADY);
1588 /* not found - create */
1589 OBD_ALLOC(buffer, LNET_NIDSTR_SIZE);
1591 GOTO(destroy_new, rc = -ENOMEM);
1593 memcpy(buffer, libcfs_nid2str(*nid), LNET_NIDSTR_SIZE);
1594 new_stat->nid_proc = lprocfs_register(buffer,
1595 obd->obd_proc_exports_entry,
1597 OBD_FREE(buffer, LNET_NIDSTR_SIZE);
1599 if (IS_ERR(new_stat->nid_proc)) {
1600 CERROR("Error making export directory for nid %s\n",
1601 libcfs_nid2str(*nid));
1602 rc = PTR_ERR(new_stat->nid_proc);
1603 new_stat->nid_proc = NULL;
1604 GOTO(destroy_new_ns, rc);
1607 entry = lprocfs_add_simple(new_stat->nid_proc, "uuid",
1608 new_stat, &lproc_exp_uuid_fops);
1609 if (IS_ERR(entry)) {
1610 CWARN("Error adding the NID stats file\n");
1611 rc = PTR_ERR(entry);
1612 GOTO(destroy_new_ns, rc);
1615 entry = lprocfs_add_simple(new_stat->nid_proc, "hash",
1616 new_stat, &lproc_exp_hash_fops);
1617 if (IS_ERR(entry)) {
1618 CWARN("Error adding the hash file\n");
1619 rc = PTR_ERR(entry);
1620 GOTO(destroy_new_ns, rc);
1623 exp->exp_nid_stats = new_stat;
1625 /* protect competitive add to list, not need locking on destroy */
1626 spin_lock(&obd->obd_nid_lock);
1627 list_add(&new_stat->nid_list, &obd->obd_nid_stats);
1628 spin_unlock(&obd->obd_nid_lock);
1633 if (new_stat->nid_proc != NULL)
1634 lprocfs_remove(&new_stat->nid_proc);
1635 cfs_hash_del(obd->obd_nid_stats_hash, nid, &new_stat->nid_hash);
1638 nidstat_putref(new_stat);
1639 OBD_FREE_PTR(new_stat);
1642 EXPORT_SYMBOL(lprocfs_exp_setup);
1644 int lprocfs_exp_cleanup(struct obd_export *exp)
1646 struct nid_stat *stat = exp->exp_nid_stats;
1648 if(!stat || !exp->exp_obd)
1651 nidstat_putref(exp->exp_nid_stats);
1652 exp->exp_nid_stats = NULL;
1656 EXPORT_SYMBOL(lprocfs_exp_cleanup);
1658 int lprocfs_write_helper(const char *buffer, unsigned long count,
1661 return lprocfs_write_frac_helper(buffer, count, val, 1);
1663 EXPORT_SYMBOL(lprocfs_write_helper);
1665 int lprocfs_write_frac_helper(const char *buffer, unsigned long count,
1668 char kernbuf[20], *end, *pbuf;
1670 if (count > (sizeof(kernbuf) - 1))
1673 if (copy_from_user(kernbuf, buffer, count))
1676 kernbuf[count] = '\0';
1683 *val = (int)simple_strtoul(pbuf, &end, 10) * mult;
1687 if (end != NULL && *end == '.') {
1688 int temp_val, pow = 1;
1692 if (strlen(pbuf) > 5)
1693 pbuf[5] = '\0'; /*only allow 5bits fractional*/
1695 temp_val = (int)simple_strtoul(pbuf, &end, 10) * mult;
1698 for (i = 0; i < (end - pbuf); i++)
1701 *val += temp_val / pow;
1706 EXPORT_SYMBOL(lprocfs_write_frac_helper);
1708 int lprocfs_read_frac_helper(char *buffer, unsigned long count, long val,
1711 long decimal_val, frac_val;
1717 decimal_val = val / mult;
1718 prtn = snprintf(buffer, count, "%ld", decimal_val);
1719 frac_val = val % mult;
1721 if (prtn < (count - 4) && frac_val > 0) {
1723 int i, temp_mult = 1, frac_bits = 0;
1725 temp_frac = frac_val * 10;
1726 buffer[prtn++] = '.';
1727 while (frac_bits < 2 && (temp_frac / mult) < 1 ) {
1728 /* only reserved 2 bits fraction */
1729 buffer[prtn++] ='0';
1734 * Need to think these cases :
1735 * 1. #echo x.00 > /proc/xxx output result : x
1736 * 2. #echo x.0x > /proc/xxx output result : x.0x
1737 * 3. #echo x.x0 > /proc/xxx output result : x.x
1738 * 4. #echo x.xx > /proc/xxx output result : x.xx
1739 * Only reserved 2 bits fraction.
1741 for (i = 0; i < (5 - prtn); i++)
1744 frac_bits = min((int)count - prtn, 3 - frac_bits);
1745 prtn += snprintf(buffer + prtn, frac_bits, "%ld",
1746 frac_val * temp_mult / mult);
1749 while(buffer[prtn] < '1' || buffer[prtn] > '9') {
1751 if (buffer[prtn] == '.') {
1758 buffer[prtn++] ='\n';
1761 EXPORT_SYMBOL(lprocfs_read_frac_helper);
1763 int lprocfs_seq_read_frac_helper(struct seq_file *m, long val, int mult)
1765 long decimal_val, frac_val;
1767 decimal_val = val / mult;
1768 seq_printf(m, "%ld", decimal_val);
1769 frac_val = val % mult;
1776 /* Three cases: x0, xx, 0x */
1777 if ((frac_val % 10) != 0)
1778 seq_printf(m, ".%ld", frac_val);
1780 seq_printf(m, ".%ld", frac_val / 10);
1783 seq_printf(m, "\n");
1786 EXPORT_SYMBOL(lprocfs_seq_read_frac_helper);
1788 int lprocfs_write_u64_helper(const char *buffer, unsigned long count,__u64 *val)
1790 return lprocfs_write_frac_u64_helper(buffer, count, val, 1);
1792 EXPORT_SYMBOL(lprocfs_write_u64_helper);
1794 int lprocfs_write_frac_u64_helper(const char *buffer, unsigned long count,
1795 __u64 *val, int mult)
1797 char kernbuf[22], *end, *pbuf;
1798 __u64 whole, frac = 0, units;
1799 unsigned frac_d = 1;
1801 if (count > (sizeof(kernbuf) - 1))
1804 if (copy_from_user(kernbuf, buffer, count))
1807 kernbuf[count] = '\0';
1814 whole = simple_strtoull(pbuf, &end, 10);
1818 if (end != NULL && *end == '.') {
1822 /* need to limit frac_d to a __u32 */
1823 if (strlen(pbuf) > 10)
1826 frac = simple_strtoull(pbuf, &end, 10);
1827 /* count decimal places */
1828 for (i = 0; i < (end - pbuf); i++)
1845 /* Specified units override the multiplier */
1847 mult = mult < 0 ? -units : units;
1850 do_div(frac, frac_d);
1851 *val = whole * mult + frac;
1854 EXPORT_SYMBOL(lprocfs_write_frac_u64_helper);
1856 static char *lprocfs_strnstr(const char *s1, const char *s2, size_t len)
1865 if (!memcmp(s1, s2, l2))
1873 * Find the string \a name in the input \a buffer, and return a pointer to the
1874 * value immediately following \a name, reducing \a count appropriately.
1875 * If \a name is not found the original \a buffer is returned.
1877 char *lprocfs_find_named_value(const char *buffer, const char *name,
1881 size_t buflen = *count;
1883 /* there is no strnstr() in rhel5 and ubuntu kernels */
1884 val = lprocfs_strnstr(buffer, name, buflen);
1886 return (char *)buffer;
1888 val += strlen(name); /* skip prefix */
1889 while (val < buffer + buflen && isspace(*val)) /* skip separator */
1893 while (val < buffer + buflen && isalnum(*val)) {
1898 return val - *count;
1900 EXPORT_SYMBOL(lprocfs_find_named_value);
1902 int lprocfs_seq_create(struct proc_dir_entry *parent,
1905 const struct file_operations *seq_fops,
1908 struct proc_dir_entry *entry;
1910 /* Disallow secretly (un)writable entries. */
1911 LASSERT((seq_fops->write == NULL) == ((mode & 0222) == 0));
1912 entry = proc_create_data(name, mode, parent, seq_fops, data);
1919 EXPORT_SYMBOL(lprocfs_seq_create);
1921 int lprocfs_obd_seq_create(struct obd_device *dev,
1924 const struct file_operations *seq_fops,
1927 return (lprocfs_seq_create(dev->obd_proc_entry, name,
1928 mode, seq_fops, data));
1930 EXPORT_SYMBOL(lprocfs_obd_seq_create);
1932 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value)
1934 if (value >= OBD_HIST_MAX)
1935 value = OBD_HIST_MAX - 1;
1937 spin_lock(&oh->oh_lock);
1938 oh->oh_buckets[value]++;
1939 spin_unlock(&oh->oh_lock);
1941 EXPORT_SYMBOL(lprocfs_oh_tally);
1943 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value)
1947 for (val = 0; ((1 << val) < value) && (val <= OBD_HIST_MAX); val++)
1950 lprocfs_oh_tally(oh, val);
1952 EXPORT_SYMBOL(lprocfs_oh_tally_log2);
1954 unsigned long lprocfs_oh_sum(struct obd_histogram *oh)
1956 unsigned long ret = 0;
1959 for (i = 0; i < OBD_HIST_MAX; i++)
1960 ret += oh->oh_buckets[i];
1963 EXPORT_SYMBOL(lprocfs_oh_sum);
1965 void lprocfs_oh_clear(struct obd_histogram *oh)
1967 spin_lock(&oh->oh_lock);
1968 memset(oh->oh_buckets, 0, sizeof(oh->oh_buckets));
1969 spin_unlock(&oh->oh_lock);
1971 EXPORT_SYMBOL(lprocfs_oh_clear);
1973 int lprocfs_obd_rd_max_pages_per_rpc(struct seq_file *m, void *data)
1975 struct obd_device *dev = data;
1976 struct client_obd *cli = &dev->u.cli;
1979 client_obd_list_lock(&cli->cl_loi_list_lock);
1980 rc = seq_printf(m, "%d\n", cli->cl_max_pages_per_rpc);
1981 client_obd_list_unlock(&cli->cl_loi_list_lock);
1984 EXPORT_SYMBOL(lprocfs_obd_rd_max_pages_per_rpc);