Whamcloud - gitweb
b=13716
[fs/lustre-release.git] / lustre / ptlrpc / lproc_ptlrpc.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of the Lustre file system, http://www.lustre.org
7  *   Lustre is a trademark of Cluster File Systems, Inc.
8  *
9  *   You may have signed or agreed to another license before downloading
10  *   this software.  If so, you are bound by the terms and conditions
11  *   of that agreement, and the following does not apply to you.  See the
12  *   LICENSE file included with this distribution for more information.
13  *
14  *   If you did not agree to a different license, then this copy of Lustre
15  *   is open source software; you can redistribute it and/or modify it
16  *   under the terms of version 2 of the GNU General Public License as
17  *   published by the Free Software Foundation.
18  *
19  *   In either case, Lustre is distributed in the hope that it will be
20  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *   license text for more details.
23  *
24  */
25 #define DEBUG_SUBSYSTEM S_CLASS
26
27 #ifndef __KERNEL__
28 # include <liblustre.h>
29 #endif
30
31 #include <obd_support.h>
32 #include <obd.h>
33 #include <lprocfs_status.h>
34 #include <lustre/lustre_idl.h>
35 #include <lustre_net.h>
36 #include <obd_class.h>
37 #include "ptlrpc_internal.h"
38
39
40 struct ll_rpc_opcode {
41      __u32       opcode;
42      const char *opname;
43 } ll_rpc_opcode_table[LUSTRE_MAX_OPCODES] = {
44         { OST_REPLY,        "ost_reply" },
45         { OST_GETATTR,      "ost_getattr" },
46         { OST_SETATTR,      "ost_setattr" },
47         { OST_READ,         "ost_read" },
48         { OST_WRITE,        "ost_write" },
49         { OST_CREATE ,      "ost_create" },
50         { OST_DESTROY,      "ost_destroy" },
51         { OST_GET_INFO,     "ost_get_info" },
52         { OST_CONNECT,      "ost_connect" },
53         { OST_DISCONNECT,   "ost_disconnect" },
54         { OST_PUNCH,        "ost_punch" },
55         { OST_OPEN,         "ost_open" },
56         { OST_CLOSE,        "ost_close" },
57         { OST_STATFS,       "ost_statfs" },
58         { 14,                NULL },    /* formerly OST_SAN_READ */
59         { 15,                NULL },    /* formerly OST_SAN_WRITE */
60         { OST_SYNC,         "ost_sync" },
61         { OST_SET_INFO,     "ost_set_info" },
62         { OST_QUOTACHECK,   "ost_quotacheck" },
63         { OST_QUOTACTL,     "ost_quotactl" },
64         { MDS_GETATTR,      "mds_getattr" },
65         { MDS_GETATTR_NAME, "mds_getattr_lock" },
66         { MDS_CLOSE,        "mds_close" },
67         { MDS_REINT,        "mds_reint" },
68         { MDS_READPAGE,     "mds_readpage" },
69         { MDS_CONNECT,      "mds_connect" },
70         { MDS_DISCONNECT,   "mds_disconnect" },
71         { MDS_GETSTATUS,    "mds_getstatus" },
72         { MDS_STATFS,       "mds_statfs" },
73         { MDS_PIN,          "mds_pin" },
74         { MDS_UNPIN,        "mds_unpin" },
75         { MDS_SYNC,         "mds_sync" },
76         { MDS_DONE_WRITING, "mds_done_writing" },
77         { MDS_SET_INFO,     "mds_set_info" },
78         { MDS_QUOTACHECK,   "mds_quotacheck" },
79         { MDS_QUOTACTL,     "mds_quotactl" },
80         { MDS_GETXATTR,     "mds_getxattr" },
81         { MDS_SETXATTR,     "mds_setxattr" },
82         { LDLM_ENQUEUE,     "ldlm_enqueue" },
83         { LDLM_CONVERT,     "ldlm_convert" },
84         { LDLM_CANCEL,      "ldlm_cancel" },
85         { LDLM_BL_CALLBACK, "ldlm_bl_callback" },
86         { LDLM_CP_CALLBACK, "ldlm_cp_callback" },
87         { LDLM_GL_CALLBACK, "ldlm_gl_callback" },
88         { MGS_CONNECT,      "mgs_connect" },
89         { MGS_DISCONNECT,   "mgs_disconnect" },
90         { MGS_EXCEPTION,    "mgs_exception" },
91         { MGS_TARGET_REG,   "mgs_target_reg" },
92         { MGS_TARGET_DEL,   "mgs_target_del" },
93         { OBD_PING,         "obd_ping" },
94         { OBD_LOG_CANCEL,   "llog_origin_handle_cancel" },
95         { OBD_QC_CALLBACK,  "obd_quota_callback" },
96         { LLOG_ORIGIN_HANDLE_CREATE,     "llog_origin_handle_create" },
97         { LLOG_ORIGIN_HANDLE_NEXT_BLOCK, "llog_origin_handle_next_block"},
98         { LLOG_ORIGIN_HANDLE_READ_HEADER,"llog_origin_handle_read_header" },
99         { LLOG_ORIGIN_HANDLE_WRITE_REC,  "llog_origin_handle_write_rec" },
100         { LLOG_ORIGIN_HANDLE_CLOSE,      "llog_origin_handle_close" },
101         { LLOG_ORIGIN_CONNECT,           "llog_origin_connect" },
102         { LLOG_CATINFO,                  "llog_catinfo" },
103         { LLOG_ORIGIN_HANDLE_PREV_BLOCK, "llog_origin_handle_prev_block" },
104         { LLOG_ORIGIN_HANDLE_DESTROY,    "llog_origin_handle_destroy" },
105 };
106
107 const char* ll_opcode2str(__u32 opcode)
108 {
109         /* When one of the assertions below fail, chances are that:
110          *     1) A new opcode was added in lustre_idl.h, but was
111          *        is missing from the table above.
112          * or  2) The opcode space was renumbered or rearranged,
113          *        and the opcode_offset() function in
114          *        ptlrpc_internal.h needs to be modified.
115          */
116         __u32 offset = opcode_offset(opcode);
117         LASSERT(offset < LUSTRE_MAX_OPCODES);
118         LASSERT(ll_rpc_opcode_table[offset].opcode == opcode);
119         return ll_rpc_opcode_table[offset].opname;
120 }
121
122 #ifdef LPROCFS
123 void ptlrpc_lprocfs_register(struct proc_dir_entry *root, char *dir,
124                              char *name, struct proc_dir_entry **procroot_ret,
125                              struct lprocfs_stats **stats_ret)
126 {
127         struct proc_dir_entry *svc_procroot;
128         struct lprocfs_stats *svc_stats;
129         int i, rc;
130         unsigned int svc_counter_config = LPROCFS_CNTR_AVGMINMAX |
131                                           LPROCFS_CNTR_STDDEV;
132
133         LASSERT(*procroot_ret == NULL);
134         LASSERT(*stats_ret == NULL);
135
136         svc_stats = lprocfs_alloc_stats(PTLRPC_LAST_CNTR + LUSTRE_MAX_OPCODES);
137         if (svc_stats == NULL)
138                 return;
139
140         if (dir) {
141                 svc_procroot = lprocfs_register(dir, root, NULL, NULL);
142                 if (IS_ERR(svc_procroot)) {
143                         lprocfs_free_stats(&svc_stats);
144                         return;
145                 }
146         } else {
147                 svc_procroot = root;
148         }
149
150         lprocfs_counter_init(svc_stats, PTLRPC_REQWAIT_CNTR,
151                              svc_counter_config, "req_waittime", "usec");
152         lprocfs_counter_init(svc_stats, PTLRPC_REQQDEPTH_CNTR,
153                              svc_counter_config, "req_qdepth", "reqs");
154         lprocfs_counter_init(svc_stats, PTLRPC_REQACTIVE_CNTR,
155                              svc_counter_config, "req_active", "reqs");
156         lprocfs_counter_init(svc_stats, PTLRPC_TIMEOUT,
157                              svc_counter_config, "adaptive_timeout", "sec");
158         lprocfs_counter_init(svc_stats, PTLRPC_REQBUF_AVAIL_CNTR,
159                              svc_counter_config, "reqbuf_avail", "bufs");
160         for (i = 0; i < LUSTRE_MAX_OPCODES; i++) {
161                 __u32 opcode = ll_rpc_opcode_table[i].opcode;
162                 lprocfs_counter_init(svc_stats, PTLRPC_LAST_CNTR + i,
163                                      svc_counter_config, ll_opcode2str(opcode),
164                                      "usec");
165         }
166
167         rc = lprocfs_register_stats(svc_procroot, name, svc_stats);
168         if (rc < 0) {
169                 if (dir)
170                         lprocfs_remove(&svc_procroot);
171                 lprocfs_free_stats(&svc_stats);
172         } else {
173                 if (dir)
174                         *procroot_ret = svc_procroot;
175                 *stats_ret = svc_stats;
176         }
177 }
178
179 static int
180 ptlrpc_lprocfs_read_req_history_len(char *page, char **start, off_t off,
181                                     int count, int *eof, void *data)
182 {
183         struct ptlrpc_service *svc = data;
184
185         *eof = 1;
186         return snprintf(page, count, "%d\n", svc->srv_n_history_rqbds);
187 }
188
189 static int
190 ptlrpc_lprocfs_read_req_history_max(char *page, char **start, off_t off,
191                                     int count, int *eof, void *data)
192 {
193         struct ptlrpc_service *svc = data;
194
195         *eof = 1;
196         return snprintf(page, count, "%d\n", svc->srv_max_history_rqbds);
197 }
198
199 static int
200 ptlrpc_lprocfs_write_req_history_max(struct file *file, const char *buffer,
201                                      unsigned long count, void *data)
202 {
203         struct ptlrpc_service *svc = data;
204         int                    bufpages;
205         int                    val;
206         int                    rc = lprocfs_write_helper(buffer, count, &val);
207
208         if (rc < 0)
209                 return rc;
210
211         if (val < 0)
212                 return -ERANGE;
213
214         /* This sanity check is more of an insanity check; we can still
215          * hose a kernel by allowing the request history to grow too
216          * far. */
217         bufpages = (svc->srv_buf_size + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
218         if (val > num_physpages/(2*bufpages))
219                 return -ERANGE;
220
221         spin_lock(&svc->srv_lock);
222         svc->srv_max_history_rqbds = val;
223         spin_unlock(&svc->srv_lock);
224
225         return count;
226 }
227
228 struct ptlrpc_srh_iterator {
229         __u64                  srhi_seq;
230         struct ptlrpc_request *srhi_req;
231 };
232
233 int
234 ptlrpc_lprocfs_svc_req_history_seek(struct ptlrpc_service *svc,
235                                     struct ptlrpc_srh_iterator *srhi,
236                                     __u64 seq)
237 {
238         struct list_head      *e;
239         struct ptlrpc_request *req;
240
241         if (srhi->srhi_req != NULL &&
242             srhi->srhi_seq > svc->srv_request_max_cull_seq &&
243             srhi->srhi_seq <= seq) {
244                 /* If srhi_req was set previously, hasn't been culled and
245                  * we're searching for a seq on or after it (i.e. more
246                  * recent), search from it onwards.
247                  * Since the service history is LRU (i.e. culled reqs will
248                  * be near the head), we shouldn't have to do long
249                  * re-scans */
250                 LASSERT (srhi->srhi_seq == srhi->srhi_req->rq_history_seq);
251                 LASSERT (!list_empty(&svc->srv_request_history));
252                 e = &srhi->srhi_req->rq_history_list;
253         } else {
254                 /* search from start */
255                 e = svc->srv_request_history.next;
256         }
257
258         while (e != &svc->srv_request_history) {
259                 req = list_entry(e, struct ptlrpc_request, rq_history_list);
260
261                 if (req->rq_history_seq >= seq) {
262                         srhi->srhi_seq = req->rq_history_seq;
263                         srhi->srhi_req = req;
264                         return 0;
265                 }
266                 e = e->next;
267         }
268
269         return -ENOENT;
270 }
271
272 static void *
273 ptlrpc_lprocfs_svc_req_history_start(struct seq_file *s, loff_t *pos)
274 {
275         struct ptlrpc_service       *svc = s->private;
276         struct ptlrpc_srh_iterator  *srhi;
277         int                          rc;
278
279         OBD_ALLOC(srhi, sizeof(*srhi));
280         if (srhi == NULL)
281                 return NULL;
282
283         srhi->srhi_seq = 0;
284         srhi->srhi_req = NULL;
285
286         spin_lock(&svc->srv_lock);
287         rc = ptlrpc_lprocfs_svc_req_history_seek(svc, srhi, *pos);
288         spin_unlock(&svc->srv_lock);
289
290         if (rc == 0) {
291                 *pos = srhi->srhi_seq;
292                 return srhi;
293         }
294
295         OBD_FREE(srhi, sizeof(*srhi));
296         return NULL;
297 }
298
299 static void
300 ptlrpc_lprocfs_svc_req_history_stop(struct seq_file *s, void *iter)
301 {
302         struct ptlrpc_srh_iterator *srhi = iter;
303
304         if (srhi != NULL)
305                 OBD_FREE(srhi, sizeof(*srhi));
306 }
307
308 static void *
309 ptlrpc_lprocfs_svc_req_history_next(struct seq_file *s,
310                                     void *iter, loff_t *pos)
311 {
312         struct ptlrpc_service       *svc = s->private;
313         struct ptlrpc_srh_iterator  *srhi = iter;
314         int                          rc;
315
316         spin_lock(&svc->srv_lock);
317         rc = ptlrpc_lprocfs_svc_req_history_seek(svc, srhi, *pos + 1);
318         spin_unlock(&svc->srv_lock);
319
320         if (rc != 0) {
321                 OBD_FREE(srhi, sizeof(*srhi));
322                 return NULL;
323         }
324
325         *pos = srhi->srhi_seq;
326         return srhi;
327 }
328
329 static int ptlrpc_lprocfs_svc_req_history_show(struct seq_file *s, void *iter)
330 {
331         struct ptlrpc_service      *svc = s->private;
332         struct ptlrpc_srh_iterator *srhi = iter;
333         struct ptlrpc_request      *req;
334         int                         rc;
335
336         spin_lock(&svc->srv_lock);
337
338         rc = ptlrpc_lprocfs_svc_req_history_seek(svc, srhi, srhi->srhi_seq);
339
340         if (rc == 0) {
341                 req = srhi->srhi_req;
342
343                 /* Print common req fields.
344                  * CAVEAT EMPTOR: we're racing with the service handler
345                  * here.  The request could contain any old crap, so you
346                  * must be just as careful as the service's request
347                  * parser. Currently I only print stuff here I know is OK
348                  * to look at coz it was set up in request_in_callback()!!! */
349                 seq_printf(s, LPD64":%s:%s:"LPD64":%d:%s ",
350                            req->rq_history_seq, libcfs_nid2str(req->rq_self), 
351                            libcfs_id2str(req->rq_peer), req->rq_xid, 
352                            req->rq_reqlen,ptlrpc_rqphase2str(req));
353
354                 if (svc->srv_request_history_print_fn == NULL)
355                         seq_printf(s, "\n");
356                 else
357                         svc->srv_request_history_print_fn(s, srhi->srhi_req);
358         }
359
360         spin_unlock(&svc->srv_lock);
361
362         return rc;
363 }
364
365 static int
366 ptlrpc_lprocfs_svc_req_history_open(struct inode *inode, struct file *file)
367 {
368         static struct seq_operations sops = {
369                 .start = ptlrpc_lprocfs_svc_req_history_start,
370                 .stop  = ptlrpc_lprocfs_svc_req_history_stop,
371                 .next  = ptlrpc_lprocfs_svc_req_history_next,
372                 .show  = ptlrpc_lprocfs_svc_req_history_show,
373         };
374         struct proc_dir_entry *dp = PDE(inode);
375         struct seq_file       *seqf;
376         int                    rc;
377
378         LPROCFS_ENTRY_AND_CHECK(dp);
379         rc = seq_open(file, &sops);
380         if (rc) {
381                 LPROCFS_EXIT();
382                 return rc;
383         }
384
385         seqf = file->private_data;
386         seqf->private = dp->data;
387         return 0;
388 }
389
390 /* See also lprocfs_rd_timeouts */
391 static int ptlrpc_lprocfs_rd_timeouts(char *page, char **start, off_t off,
392                                       int count, int *eof, void *data)
393 {
394         struct ptlrpc_service *svc = data;
395         unsigned int cur, worst;
396         time_t worstt;
397         struct dhms ts;
398         int rc = 0;
399
400         *eof = 1;
401         cur = at_get(&svc->srv_at_estimate);
402         worst = svc->srv_at_estimate.at_worst_ever;
403         worstt = svc->srv_at_estimate.at_worst_time;
404         s2dhms(&ts, cfs_time_current_sec() - worstt);
405         if (AT_OFF)
406                 rc += snprintf(page + rc, count - rc,
407                               "adaptive timeouts off, using obd_timeout %u\n",
408                               obd_timeout);
409         rc += snprintf(page + rc, count - rc, 
410                        "%10s : cur %3u  worst %3u (at %ld, "DHMS_FMT" ago) ",
411                        "service", cur, worst, worstt, 
412                        DHMS_VARS(&ts));
413         rc = lprocfs_at_hist_helper(page, count, rc,
414                                     &svc->srv_at_estimate);
415         return rc;
416 }               
417
418 void ptlrpc_lprocfs_register_service(struct proc_dir_entry *entry,
419                                      struct ptlrpc_service *svc)
420 {
421         struct lprocfs_vars lproc_vars[] = {
422                 {.name       = "req_buffer_history_len",
423                  .write_fptr = NULL,
424                  .read_fptr  = ptlrpc_lprocfs_read_req_history_len,
425                  .data       = svc},
426                 {.name       = "req_buffer_history_max",
427                  .write_fptr = ptlrpc_lprocfs_write_req_history_max,
428                  .read_fptr  = ptlrpc_lprocfs_read_req_history_max,
429                  .data       = svc},
430                 {.name       = "timeouts",
431                  .read_fptr  = ptlrpc_lprocfs_rd_timeouts,
432                  .data       = svc},
433                 {NULL}
434         };
435         static struct file_operations req_history_fops = {
436                 .owner       = THIS_MODULE,
437                 .open        = ptlrpc_lprocfs_svc_req_history_open,
438                 .read        = seq_read,
439                 .llseek      = seq_lseek,
440                 .release     = lprocfs_seq_release,
441         };
442         struct proc_dir_entry *req_history;
443
444         ptlrpc_lprocfs_register(entry, svc->srv_name,
445                                 "stats", &svc->srv_procroot,
446                                 &svc->srv_stats);
447
448         if (svc->srv_procroot == NULL)
449                 return;
450
451         lprocfs_add_vars(svc->srv_procroot, lproc_vars, NULL);
452
453         req_history = create_proc_entry("req_history", 0400,
454                                         svc->srv_procroot);
455         if (req_history != NULL) {
456                 req_history->data = svc;
457                 req_history->proc_fops = &req_history_fops;
458         }
459 }
460
461 void ptlrpc_lprocfs_register_obd(struct obd_device *obddev)
462 {
463         ptlrpc_lprocfs_register(obddev->obd_proc_entry, NULL, "stats",
464                                 &obddev->obd_svc_procroot,
465                                 &obddev->obd_svc_stats);
466 }
467 EXPORT_SYMBOL(ptlrpc_lprocfs_register_obd);
468
469 void ptlrpc_lprocfs_rpc_sent(struct ptlrpc_request *req)
470 {
471         struct lprocfs_stats *svc_stats;
472         int opc = opcode_offset(lustre_msg_get_opc(req->rq_reqmsg));
473
474         svc_stats = req->rq_import->imp_obd->obd_svc_stats;
475         if (svc_stats == NULL || opc <= 0)
476                 return;
477         LASSERT(opc < LUSTRE_MAX_OPCODES);
478         /* These two use the ptlrpc_lprocfs_brw below */
479         if (!(opc == OST_WRITE || opc == OST_READ))
480                 lprocfs_counter_add(svc_stats, opc + PTLRPC_LAST_CNTR, 0);
481 }
482
483 void ptlrpc_lprocfs_brw(struct ptlrpc_request *req, int opc, int bytes)
484 {
485         struct lprocfs_stats *svc_stats;
486         svc_stats = req->rq_import->imp_obd->obd_svc_stats;
487         if (!svc_stats) 
488                 return;
489         lprocfs_counter_add(svc_stats, opc + PTLRPC_LAST_CNTR, bytes);
490 }
491 EXPORT_SYMBOL(ptlrpc_lprocfs_brw);
492
493 void ptlrpc_lprocfs_unregister_service(struct ptlrpc_service *svc)
494 {
495         if (svc->srv_procroot != NULL) 
496                 lprocfs_remove(&svc->srv_procroot);
497         if (svc->srv_stats) 
498                 lprocfs_free_stats(&svc->srv_stats);
499 }
500
501 void ptlrpc_lprocfs_unregister_obd(struct obd_device *obd)
502 {
503         if (obd->obd_svc_procroot)
504                 lprocfs_remove(&obd->obd_svc_procroot);
505         if (obd->obd_svc_stats)
506                 lprocfs_free_stats(&obd->obd_svc_stats);
507 }
508 EXPORT_SYMBOL(ptlrpc_lprocfs_unregister_obd);
509
510
511 int lprocfs_wr_evict_client(struct file *file, const char *buffer,
512                             unsigned long count, void *data)
513 {
514         struct obd_device *obd = data;
515         char tmpbuf[sizeof(struct obd_uuid)];
516
517         /* Kludge code(deadlock situation): the lprocfs lock has been held 
518          * since the client is evicted by writting client's
519          * uuid/nid to procfs "evict_client" entry. However, 
520          * obd_export_evict_by_uuid() will call lprocfs_remove() to destroy
521          * the proc entries under the being destroyed export{}, so I have
522          * to drop the lock at first here. 
523          * - jay, jxiong@clusterfs.com */
524         class_incref(obd);
525         LPROCFS_EXIT();
526
527         sscanf(buffer, "%40s", tmpbuf);
528         if (strncmp(tmpbuf, "nid:", 4) == 0)
529                 obd_export_evict_by_nid(obd, tmpbuf + 4);
530         else if (strncmp(tmpbuf, "uuid:", 5) == 0)
531                 obd_export_evict_by_uuid(obd, tmpbuf + 5);
532         else
533                 obd_export_evict_by_uuid(obd, tmpbuf);
534
535         LPROCFS_ENTRY();
536         class_decref(obd);
537
538         return count;
539 }
540 EXPORT_SYMBOL(lprocfs_wr_evict_client);
541
542 int lprocfs_wr_ping(struct file *file, const char *buffer,
543                     unsigned long count, void *data)
544 {
545         struct obd_device *obd = data;
546         struct ptlrpc_request *req;
547         int rc;
548         ENTRY;
549
550         LPROCFS_CLIMP_CHECK(obd);
551         req = ptlrpc_prep_req(obd->u.cli.cl_import, LUSTRE_OBD_VERSION,
552                               OBD_PING, 1, NULL, NULL);
553         LPROCFS_CLIMP_EXIT(obd);
554         if (req == NULL)
555                 RETURN(-ENOMEM);
556
557         ptlrpc_req_set_repsize(req, 1, NULL);
558         req->rq_send_state = LUSTRE_IMP_FULL;
559         req->rq_no_resend = 1;
560
561         rc = ptlrpc_queue_wait(req);
562
563         ptlrpc_req_finished(req);
564         if (rc >= 0)
565                 RETURN(count);
566         RETURN(rc);
567 }
568 EXPORT_SYMBOL(lprocfs_wr_ping);
569
570 #endif /* LPROCFS */