V4L/DVB (10252): v4l2 doc: explain why v4l2_device_unregister_subdev() has to be...
[safe/jmp/linux-2.6] / Documentation / accounting / getdelays.c
index e9126e7..7ea2311 100644 (file)
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/socket.h>
-#include <sys/types.h>
 #include <signal.h>
 
 #include <linux/genetlink.h>
 #include <linux/taskstats.h>
+#include <linux/cgroupstats.h>
 
 /*
  * Generic macros for dealing with netlink sockets. Might be duplicated
@@ -49,6 +49,7 @@ char name[100];
 int dbg;
 int print_delays;
 int print_io_accounting;
+int print_task_context_switch_counts;
 __u64 stime, utime;
 
 #define PRINTF(fmt, arg...) {                  \
@@ -61,8 +62,6 @@ __u64 stime, utime;
 #define MAX_MSG_SIZE   1024
 /* Maximum number of cpus expected to be specified in a cpumask */
 #define MAX_CPUS       32
-/* Maximum length of pathname to log file */
-#define MAX_FILENAME   256
 
 struct msgtemplate {
        struct nlmsghdr n;
@@ -72,6 +71,17 @@ struct msgtemplate {
 
 char cpumask[100+6*MAX_CPUS];
 
+static void usage(void)
+{
+       fprintf(stderr, "getdelays [-dilv] [-w logfile] [-r bufsize] "
+                       "[-m cpumask] [-t tgid] [-p pid]\n");
+       fprintf(stderr, "  -d: print delayacct stats\n");
+       fprintf(stderr, "  -i: print IO accounting (works only with -p)\n");
+       fprintf(stderr, "  -l: listen forever\n");
+       fprintf(stderr, "  -v: debug on\n");
+       fprintf(stderr, "  -C: container path\n");
+}
+
 /*
  * Create a raw netlink socket and bind
  */
@@ -158,7 +168,7 @@ int get_family_id(int sd)
                char buf[256];
        } ans;
 
-       int id, rc;
+       int id = 0, rc;
        struct nlattr *na;
        int rep_len;
 
@@ -186,16 +196,45 @@ void print_delayacct(struct taskstats *t)
               "      %15llu%15llu%15llu%15llu\n"
               "IO    %15s%15s\n"
               "      %15llu%15llu\n"
-              "MEM   %15s%15s\n"
-              "      %15llu%15llu\n\n",
+              "SWAP  %15s%15s\n"
+              "      %15llu%15llu\n"
+              "RECLAIM  %12s%15s\n"
+              "      %15llu%15llu\n",
               "count", "real total", "virtual total", "delay total",
-              t->cpu_count, t->cpu_run_real_total, t->cpu_run_virtual_total,
-              t->cpu_delay_total,
+              (unsigned long long)t->cpu_count,
+              (unsigned long long)t->cpu_run_real_total,
+              (unsigned long long)t->cpu_run_virtual_total,
+              (unsigned long long)t->cpu_delay_total,
+              "count", "delay total",
+              (unsigned long long)t->blkio_count,
+              (unsigned long long)t->blkio_delay_total,
               "count", "delay total",
-              t->blkio_count, t->blkio_delay_total,
-              "count", "delay total", t->swapin_count, t->swapin_delay_total);
+              (unsigned long long)t->swapin_count,
+              (unsigned long long)t->swapin_delay_total,
+              "count", "delay total",
+              (unsigned long long)t->freepages_count,
+              (unsigned long long)t->freepages_delay_total);
 }
 
+void task_context_switch_counts(struct taskstats *t)
+{
+       printf("\n\nTask   %15s%15s\n"
+              "       %15llu%15llu\n",
+              "voluntary", "nonvoluntary",
+              (unsigned long long)t->nvcsw, (unsigned long long)t->nivcsw);
+}
+
+void print_cgroupstats(struct cgroupstats *c)
+{
+       printf("sleeping %llu, blocked %llu, running %llu, stopped %llu, "
+               "uninterruptible %llu\n", (unsigned long long)c->nr_sleeping,
+               (unsigned long long)c->nr_io_wait,
+               (unsigned long long)c->nr_running,
+               (unsigned long long)c->nr_stopped,
+               (unsigned long long)c->nr_uninterruptible);
+}
+
+
 void print_ioacct(struct taskstats *t)
 {
        printf("%s: read=%llu, write=%llu, cancelled_write=%llu\n",
@@ -221,13 +260,16 @@ int main(int argc, char *argv[])
        int count = 0;
        int write_file = 0;
        int maskset = 0;
-       char logfile[128];
+       char *logfile = NULL;
        int loop = 0;
+       int containerset = 0;
+       char containerpath[1024];
+       int cfd = 0;
 
        struct msgtemplate msg;
 
        while (1) {
-               c = getopt(argc, argv, "diw:r:m:t:p:v:l");
+               c = getopt(argc, argv, "qdiw:r:m:t:p:vlC:");
                if (c < 0)
                        break;
 
@@ -240,8 +282,16 @@ int main(int argc, char *argv[])
                        printf("printing IO accounting\n");
                        print_io_accounting = 1;
                        break;
+               case 'q':
+                       printf("printing task/process context switch rates\n");
+                       print_task_context_switch_counts = 1;
+                       break;
+               case 'C':
+                       containerset = 1;
+                       strncpy(containerpath, optarg, strlen(optarg) + 1);
+                       break;
                case 'w':
-                       strncpy(logfile, optarg, MAX_FILENAME);
+                       logfile = strdup(optarg);
                        printf("write to file %s\n", logfile);
                        write_file = 1;
                        break;
@@ -277,7 +327,7 @@ int main(int argc, char *argv[])
                        loop = 1;
                        break;
                default:
-                       printf("Unknown option %d\n", c);
+                       usage();
                        exit(-1);
                }
        }
@@ -314,6 +364,11 @@ int main(int argc, char *argv[])
                }
        }
 
+       if (tid && containerset) {
+               fprintf(stderr, "Select either -t or -C, not both\n");
+               goto err;
+       }
+
        if (tid) {
                rc = send_cmd(nl_sd, id, mypid, TASKSTATS_CMD_GET,
                              cmd_type, &tid, sizeof(__u32));
@@ -324,6 +379,24 @@ int main(int argc, char *argv[])
                }
        }
 
+       if (containerset) {
+               cfd = open(containerpath, O_RDONLY);
+               if (cfd < 0) {
+                       perror("error opening container file");
+                       goto err;
+               }
+               rc = send_cmd(nl_sd, id, mypid, CGROUPSTATS_CMD_GET,
+                             CGROUPSTATS_CMD_ATTR_FD, &cfd, sizeof(__u32));
+               if (rc < 0) {
+                       perror("error sending cgroupstats command");
+                       goto err;
+               }
+       }
+       if (!maskset && !tid && !containerset) {
+               usage();
+               goto err;
+       }
+
        do {
                int i;
 
@@ -343,7 +416,7 @@ int main(int argc, char *argv[])
                        goto done;
                }
 
-               PRINTF("nlmsghdr size=%d, nlmsg_len=%d, rep_len=%d\n",
+               PRINTF("nlmsghdr size=%zu, nlmsg_len=%d, rep_len=%d\n",
                       sizeof(struct nlmsghdr), msg.n.nlmsg_len, rep_len);
 
 
@@ -381,6 +454,8 @@ int main(int argc, char *argv[])
                                                        print_delayacct((struct taskstats *) NLA_DATA(na));
                                                if (print_io_accounting)
                                                        print_ioacct((struct taskstats *) NLA_DATA(na));
+                                               if (print_task_context_switch_counts)
+                                                       task_context_switch_counts((struct taskstats *) NLA_DATA(na));
                                                if (fd) {
                                                        if (write(fd, NLA_DATA(na), na->nla_len) < 0) {
                                                                err(1,"write error\n");
@@ -400,6 +475,9 @@ int main(int argc, char *argv[])
                                }
                                break;
 
+                       case CGROUPSTATS_TYPE_CGROUP_STATS:
+                               print_cgroupstats(NLA_DATA(na));
+                               break;
                        default:
                                fprintf(stderr, "Unknown nla_type %d\n",
                                        na->nla_type);
@@ -421,5 +499,7 @@ err:
        close(nl_sd);
        if (fd)
                close(fd);
+       if (cfd)
+               close(cfd);
        return 0;
 }