Whamcloud - gitweb
LU-5162 mdc: Add exception entry check for radix_tree
[fs/lustre-release.git] / lustre / utils / ltrack_stats.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * lustre/utils/ltrack_stats.c
35  *
36  * Author: Milind Dumbare <milind@clusterfs.com>
37  */
38
39 #include <sys/types.h>
40 #include <sys/wait.h>
41 #include <getopt.h>
42 #include <stdio.h>
43 #include <unistd.h>
44 #include <glob.h>
45 #include <signal.h>
46 #include <string.h>
47 #include <stdlib.h>
48 #include <errno.h>
49
50 #define TRACK_BY_GID 0
51 #define TRACK_BY_PPID 1
52 #define TRACK_BY_PID 2
53 #define TRACK_FOR_ALL 3
54
55 /* We can have at the most 1024 llstats monitoring 1024 lustre clients
56  * at a time */
57 #define NO_OF_CLIENTS 1024
58
59 /* length of absolute path of vfs_ops_stats */
60 #define LEN_STATS 1024
61
62 /* Length of llstat command with all its switches and command line options */
63 #define LEN_LLSTAT (25 + LEN_STATS)
64
65 /* strlen of each lustre client entry in /proc/fs/lustre/llite/ */
66 #define LEN_CLIENT 1024
67
68 /* size of output of llstat command we read at a time */
69 #define LLSTAT_READ_SIZE 1024
70
71 /* Length of command given on command line */
72 #define COMM_LEN 4096
73
74 /* print usage */
75 void print_usage()
76 {
77         printf("Usage:\n\n");
78         printf("ltrack_stats runs command given and does one of the "
79                 "following:\n"
80                 "\t1. Writes its pid to "
81                 "/proc/fs/lustre/llite/.../stats_track_pid\n"
82                 " to collects stats for that process.\n"
83                 "\t2. Writes its ppid to "
84                 "/proc/fs/lustre/llite/.../stats_track_ppid\n"
85                 " to collect stats of that process and all its children \n"
86                 "\t3. Sets gid of process to some random gid (444) and also\n"
87                 " writes that to/proc/fs/lustre/llite/.../stats_track_gid to"
88                 " collect stats \nof all processes in that group\n\n"
89                 " It also uses llstat to generate output with interval of 1 "
90                 " second and duration\n of run of command for plot-llstat to "
91                 "generate a graph\n\n");
92         printf("ltrack_stats [-l filename] [-g <gid> | -a | -i | -c | -h ]\n"
93                "\t<command with arguments ...>\n\n");
94         printf("-l: outputs the llstat.pl's output to given <filename>"
95                "with interval of 1 second \nbetween each output and flag for"
96                "graphable output. If -l flag is not given llstat \nwont be"
97                "executed\n\n");
98
99         printf("-g: for displaying VFS operation statistics collected"
100                "for all processes having \ngroup id as given <gid> \n\n");
101
102         printf("-a: for displaying VFS operations statistics collected"
103                "for all processes\n\n");
104
105         printf("-i: for displaying VFS operation statistics collected"
106                "for only given <command's> \nPID.\n\n");
107
108         printf("-c: for displaying VFS operation statistics collected"
109                " for all processes whose \nparents' PID is same as pid of "
110                "<command> to be executed\n\n");
111
112         printf("-h: for showing this help\n");
113 }
114
115 /* - type: to which file data should be written to track VFS operations
116  * statistics
117  * - id: we either need to write gid which is given on command line or pid
118  * to collect statistics of that process or its children. */
119
120 void write_track_xid(int type, unsigned short id, char* stats_path)
121 {
122         FILE *fp;
123
124         /* for loop is used if we have more than one lustre clients on same
125          * machine. glob() return /proc entry for each lustre client */
126
127         switch(type) {
128                 case TRACK_BY_GID:
129                         strcat(stats_path, "/stats_track_gid");
130                         break;
131                 case TRACK_BY_PPID:
132                         strcat(stats_path, "/stats_track_ppid");
133                         break;
134                 case TRACK_BY_PID:
135                         strcat(stats_path, "/stats_track_pid");
136                         break;
137         }
138
139         fp = fopen(stats_path, "w+");
140         if (!fp) {
141                 fprintf(stderr, "Error: Couldn't open /proc entry file: %s\n",
142                         stats_path);
143                 exit(1);
144         }
145         if (fprintf(fp, "%d", id) < 0) {
146                 fprintf(stderr, "Error: Couldn't write id to tracking /proc"
147                         "entry file: %s\n", stats_path);
148                 exit(1);
149         }
150         if (fclose(fp)) {
151                 fprintf(stderr, "Error: Couldn't close tracking /proc entry"
152                         " file: %s\n", stats_path);
153                 exit(1);
154         }
155 }
156
157 /* Get extra command lines concatenated to "command Function getopt scans
158  *  one switch and its optional argument. So if command line is 
159  * "-g 1234 make bzImage" 1234 is optarg and "make bzImage" will be collected
160  *  with following function to exec it. */
161 char* get_command_from_argv(int optind, int argc, char** argv,char* optarg,
162                             char* command)
163 {
164         int index = 0;
165
166         strcpy(command, optarg);
167         strcat(command, " ");
168         for (index = optind; index < argc; index++) {
169                 strcat(command, argv[index]);
170                 strcat(command, " ");
171         }
172         if (strlen(command) == 1) {
173                 fprintf(stderr,"Error: command missing \n");
174                 print_usage();
175                 exit(1);
176         } else if (strlen(command) > COMM_LEN) {
177                 fprintf(stderr,"Error: Too long command \n");
178                 print_usage();
179                 exit(1);
180         }
181
182         return command;
183 }
184
185 /* Check for the llstat command in $PATH env variable. */
186 void check_llstat()
187 {
188         int status;
189
190         status = system("which llstat.pl &> /dev/null");
191         if (status) {
192                 fprintf(stderr,"Error: llstat.pl not found in PATH\n");
193                 exit(1);
194         }
195 }
196
197 pid_t fork_llstat_command(char* llstat_file,char* stats_path)
198 {
199         char truncate_command[100];
200         char llstat_command[LEN_LLSTAT];
201         pid_t pid_llstat_command;
202         FILE *fp_popen, *fp_out;
203         char buffer[LLSTAT_READ_SIZE];
204         int ret;
205         
206         /* Truncating llstat output file as it will be opened in while
207          * loop to append output */
208         sprintf(truncate_command,"> %s",llstat_file);
209         if ((ret = system(truncate_command)) != 0) {
210                 ret = WEXITSTATUS(ret);
211                 printf("error excuting truncate command: %d\n", ret);
212                 exit(ret);
213         }
214
215         strcat(stats_path, "/stats");
216
217         /* creating a string of llstat command to give to
218          * popen */
219         sprintf(llstat_command, "llstat -i 1 -g %s ",
220                 stats_path);
221
222         /* fork for llstat */
223         if ((pid_llstat_command = fork()) < 0)
224                 fprintf(stderr, "Error: Fork error\n");
225
226         /* in child (llstat) */
227         if (pid_llstat_command == 0) {
228                 /* Doing popen for llstat command */
229                 fp_popen = popen(llstat_command, "r");
230                 if (fp_popen == NULL) {
231                         fprintf(stderr,"Couldn't popen the llstat command:"
232                                 "\"%s\"n", llstat_command);
233                         exit(1);
234                 }
235                 while (fgets(buffer, LLSTAT_READ_SIZE, fp_popen) != NULL) {
236                         /* Following code should be in while loop as llstat
237                          * will keep on sending output each second and will
238                          * not exit on itself. It will be killed when we finsh
239                          * with our command so we must make the output file
240                          * consistent after writing each 1024 bytes chunk */
241
242                         /* opening file where llstat will write its output */
243                         fp_out = fopen(llstat_file, "a");
244                         if (!fp_out) {
245                                 fprintf(stderr, "Error: Couldn't open llstat"
246                                         "outfile file: %s\n",
247                                         llstat_file);
248                                 exit(1);
249                         }
250                         /* fgets reads the popen output and fprintf writes it to
251                          * output file */
252
253                         if (fputs(buffer, fp_out) == EOF) {
254                                 fprintf(stderr, "Error: Couldn't write output"
255                                         "of llstat to out file\n");
256                                 exit(1);
257                         }
258
259                         /* closing file opened for storing llstat's output */
260                         if (fclose(fp_out)) {
261                                 fprintf(stderr, "Error: Couldn't close llstat"
262                                         "outfile: %s\n", llstat_file);
263                                 exit(1);
264                         }
265                 }
266                 /* closing popen for llstat */
267                 if (pclose(fp_popen) < 0) {
268                         fprintf(stderr, "Error: Couldn't pclos"
269                                 " llstat popen call\n");
270                         exit(1);
271                 }
272         } /* child ends */
273         return pid_llstat_command;
274 }
275
276 pid_t fork_actual_command(int type, unsigned short id, char* stats_path,
277                           char* command)
278 {
279         pid_t pid;
280
281         /* starting ltrack_stats functionality here */
282         if ((pid = fork()) < 0)
283                 fprintf(stderr, "Error: Fork error\n");
284
285         /* fork for executing command */
286         if (pid == 0) {
287                 switch(type) {
288                         case TRACK_BY_GID:
289                                 if (setgid(id) < 0) {
290                                         fprintf(stderr, "Error: failed to"
291                                                " setgid\n");
292                                         exit(1);
293                                 }
294                                 pid = id;
295                                 break;
296
297                         case TRACK_BY_PID:
298                         case TRACK_BY_PPID:
299                                 pid = getpid();
300                                 break;
301
302                         /* 0 has to be written to vfs_track_pid to collect 
303                          * statistics of all processes */
304                         case TRACK_FOR_ALL:
305                                 pid = 0;
306                                 type = TRACK_BY_PID;
307                                 break;
308                 }
309                 write_track_xid(type, pid, stats_path);
310                 execl("/bin/sh", "sh", "-c", command, (char *)0);
311                 exit(0);
312         } /* child ends */
313
314         return(pid);
315 }
316
317 char* get_path_stats(int with_llstat, char* stats_path)
318 {
319         glob_t stats_glob_buffer;
320         int choice;
321         char error = 0;
322         int i;
323
324         /* No slots reserved in gl_pathv. Store the found path at 0 location */
325         stats_glob_buffer.gl_offs = 0;
326
327         /* doing glob() for attaching llstat to monitor each vfs_ops_stat for
328          * mulitiple lustre clients */
329         if (glob("/proc/fs/lustre/llite/*", GLOB_DOOFFS, NULL,
330                  &stats_glob_buffer) != 0) {
331                 fprintf(stderr,"Error: Couldn't find /proc entry for "
332                         "lustre\n");
333                 exit(1);
334         }
335
336         /* If multiple client entries found in /proc/fs/lustre/llite user will
337          * be prompted with choice of all */
338         if (stats_glob_buffer.gl_pathc > 1 && with_llstat) {
339                 check_llstat(); 
340                 printf("Multiple lustre clients found, continuing... \n");
341                 do {
342                         /* If flow is here again it means there was an error
343                          * and notifying that to user */
344                         if (error) {
345                                 int ret;
346                                 if ((ret = system("clear")) != 0) {
347                                         ret = WEXITSTATUS(ret);
348                                         printf("error excuting clear command: %d\n", ret);
349                                         exit(ret);
350                                 }
351                                 fprintf(stderr, "Error: Please give correct "
352                                         "choice.\n");
353                         }
354                         /* Simple menu based interface to avoid possible
355                          * spelling mistakes */
356                         printf("\t\tMenu.\n");
357                         for (i = 0; i < stats_glob_buffer.gl_pathc; i++)
358                                 printf("\t\t%d. %s\n", i+1, 
359                                        stats_glob_buffer.gl_pathv[i]);
360
361                         printf("\nEnter the lustre client number you want to "
362                                "use:");
363                         if (scanf(" %d", &choice) == EOF && ferror(stdin)) {
364                                 perror("reading from stdin");
365                                 exit(-1);
366                         }
367                         error++;
368                 } while (choice > stats_glob_buffer.gl_pathc || choice < 1);
369                 strcpy(stats_path, stats_glob_buffer.gl_pathv[choice - 1]);
370         } else {
371                 /*if only one client then simply copying the path from glob */
372                 strcpy(stats_path, stats_glob_buffer.gl_pathv[0]);
373         }
374         /* this frees dynamically allocated space by glob() for storing found
375          * paths */
376         globfree(&stats_glob_buffer);
377
378         return stats_path;
379 }
380
381 /* Writes the id (gid/ pid/ ppid) value in appropriate tracking proc entry file
382  * and EXECs the command given */
383 void fork_command(int type, unsigned short id, char* command, char* llstat_file)
384 {
385         pid_t pid_actual_command = 0;
386         pid_t pid_llstat_command = 0;
387
388         /* counters */
389         int with_llstat = 1;
390         int status;
391         char stats_path[1024];
392         char stats_path_temp[1024];
393
394         if (strlen(llstat_file) == 0)
395                 with_llstat = 0;
396
397         get_path_stats(with_llstat, stats_path);
398         strcpy(stats_path_temp, stats_path);
399
400         /* llstat process attached to monitor given command */
401         if (with_llstat)
402                 pid_llstat_command = fork_llstat_command(llstat_file,
403                                                          stats_path_temp);
404
405         /* forking a process which will exec command given */
406         pid_actual_command = fork_actual_command(type, id, stats_path,
407                                                  command);
408
409         if (waitpid(pid_actual_command, NULL, 0) != pid_actual_command)
410                 fprintf(stderr, "Error: waitpid error\n");
411
412         if (with_llstat) {
413                 /* comment #25 of BUG 10968 */
414                 sleep(2); 
415
416                 /* sending kill to all llstat commands created for each
417                  * lustre-client respectively */
418                 kill(pid_llstat_command, 9);
419                 waitpid(pid_llstat_command, &status, 0);
420
421                 /* if llstat child is killed by KILL only then print note for
422                  * plotting graph and if its exited normally with errornous
423                  * status then it means there were some error and llstat was
424                  * aborted*/
425                 if (!WIFEXITED(status))
426                         printf("\n\t[Note: Do \"$plot-llstat %s\" to plot a graph"
427                                " using GNU plot]\n", llstat_file);
428
429         }
430 }
431
432 /* main */
433 int main(int argc, char **argv)
434 {
435         char gid_string[5] = "";
436         gid_t gid;
437         int c;
438         char command[COMM_LEN] = "";
439         char llstat_file[100] = "";
440
441         /* Checking for root*/
442         if (getuid()) {
443                 fprintf(stderr, "Error: You need to be root\n");
444                 exit(1);
445         }
446
447         opterr = 0;
448         /* Parsing command line switches */
449         while ((c = getopt(argc, argv, "l:g:c:i:a:h")) != 1)
450                 switch (c) {
451                         case 'l':
452                                 if (strlen(optarg) > sizeof(llstat_file)-1) {
453                                         fprintf(stderr, "length of outfile file"
454                                                 " is too long\n");
455                                         exit(1);
456                                 }
457                                 strncpy(llstat_file, optarg,
458                                         sizeof(llstat_file));
459                                 break;
460
461                         /* When any value is written to vfs_track_gid, then VFS
462                          * operation statistics are collected for all
463                          * processes of that group ID.
464                          * write_track_xid writes given <gid> in vfs_track_gid
465                          * here. */
466                         case 'g':
467                                 if (strlen(optarg) > sizeof(gid_string)-1)
468                                         return -E2BIG;
469                                 strncpy(gid_string, optarg, sizeof(gid_string));
470                                 get_command_from_argv(optind, argc, argv, "",
471                                                       command);
472                                 gid = atoi(gid_string);
473
474                                 fork_command(TRACK_BY_GID, gid, command,
475                                              llstat_file); 
476                                 return(0);
477
478                         /* When any value is written to vfs_track_ppid, then VFS
479                          * operation statistics are collected for all processes
480                          * whose parents' PID is same as track_ppid.
481                          *- write_track_xid writes pid to vfs_track_ppid here */
482                         case 'c':
483                                 get_command_from_argv(optind, argc, argv,
484                                                       optarg, command);
485                                 fork_command(TRACK_BY_PPID, 0, command,
486                                              llstat_file);
487                                 return(0);
488
489                         /* When a non-zero value is written to vfs_track_pid,
490                          * then VFS operation statistics are collected for only
491                          * that PID.Write_track_xid writes pid to vfs_track_pid
492                          * here.Wei need to FORK a new process and EXEC it with
493                          * given <command>. */
494                         case 'i':
495                                 get_command_from_argv(optind, argc, argv,
496                                                       optarg, command);
497                                 fork_command(TRACK_BY_PID, 0, command,
498                                              llstat_file);
499                                 return(0);
500
501                         /* When VFS operation statistics for all processes are
502                          * to be collected, "0" is written to vfs_track_pid. */
503                         case 'a':
504                                 get_command_from_argv(optind, argc, argv,
505                                                       optarg, command);
506                                 fork_command(TRACK_FOR_ALL, 0, command,
507                                              llstat_file);
508                                 return(0);
509
510                         /* Help */
511                         case 'h':
512                                 print_usage();
513                                 return(0);
514
515                         default:
516                                 print_usage();
517                                 return(1);
518                 }
519         return(0);
520 } /* main ends */