~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/samples/bpf/lathist_user.c

Version: ~ [ linux-5.1-rc5 ] ~ [ linux-5.0.7 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.34 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.111 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.168 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.178 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.138 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.65 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /* Copyright (c) 2013-2015 PLUMgrid, http://plumgrid.com
  2  * Copyright (c) 2015 BMW Car IT GmbH
  3  *
  4  * This program is free software; you can redistribute it and/or
  5  * modify it under the terms of version 2 of the GNU General Public
  6  * License as published by the Free Software Foundation.
  7  */
  8 #include <stdio.h>
  9 #include <unistd.h>
 10 #include <stdlib.h>
 11 #include <signal.h>
 12 #include <linux/bpf.h>
 13 #include <bpf/bpf.h>
 14 #include "bpf_load.h"
 15 
 16 #define MAX_ENTRIES     20
 17 #define MAX_CPU         4
 18 #define MAX_STARS       40
 19 
 20 struct cpu_hist {
 21         long data[MAX_ENTRIES];
 22         long max;
 23 };
 24 
 25 static struct cpu_hist cpu_hist[MAX_CPU];
 26 
 27 static void stars(char *str, long val, long max, int width)
 28 {
 29         int i;
 30 
 31         for (i = 0; i < (width * val / max) - 1 && i < width - 1; i++)
 32                 str[i] = '*';
 33         if (val > max)
 34                 str[i - 1] = '+';
 35         str[i] = '\0';
 36 }
 37 
 38 static void print_hist(void)
 39 {
 40         char starstr[MAX_STARS];
 41         struct cpu_hist *hist;
 42         int i, j;
 43 
 44         /* clear screen */
 45         printf("\033[2J");
 46 
 47         for (j = 0; j < MAX_CPU; j++) {
 48                 hist = &cpu_hist[j];
 49 
 50                 /* ignore CPUs without data (maybe offline?) */
 51                 if (hist->max == 0)
 52                         continue;
 53 
 54                 printf("CPU %d\n", j);
 55                 printf("      latency        : count     distribution\n");
 56                 for (i = 1; i <= MAX_ENTRIES; i++) {
 57                         stars(starstr, hist->data[i - 1], hist->max, MAX_STARS);
 58                         printf("%8ld -> %-8ld : %-8ld |%-*s|\n",
 59                                 (1l << i) >> 1, (1l << i) - 1,
 60                                 hist->data[i - 1], MAX_STARS, starstr);
 61                 }
 62         }
 63 }
 64 
 65 static void get_data(int fd)
 66 {
 67         long key, value;
 68         int c, i;
 69 
 70         for (i = 0; i < MAX_CPU; i++)
 71                 cpu_hist[i].max = 0;
 72 
 73         for (c = 0; c < MAX_CPU; c++) {
 74                 for (i = 0; i < MAX_ENTRIES; i++) {
 75                         key = c * MAX_ENTRIES + i;
 76                         bpf_map_lookup_elem(fd, &key, &value);
 77 
 78                         cpu_hist[c].data[i] = value;
 79                         if (value > cpu_hist[c].max)
 80                                 cpu_hist[c].max = value;
 81                 }
 82         }
 83 }
 84 
 85 int main(int argc, char **argv)
 86 {
 87         char filename[256];
 88 
 89         snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
 90 
 91         if (load_bpf_file(filename)) {
 92                 printf("%s", bpf_log_buf);
 93                 return 1;
 94         }
 95 
 96         while (1) {
 97                 get_data(map_fd[1]);
 98                 print_hist();
 99                 sleep(5);
100         }
101 
102         return 0;
103 }
104 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp