Esempio n. 1
0
static void four_cpu_numbers(int *uret, int *nret, int *sret, int *iret)
{
     int       tmp_u, tmp_n, tmp_s, tmp_i;
     static JT old_u, old_n, old_s, old_i, old_wa, old_hi, old_si;
     JT        new_u, new_n, new_s, new_i, new_wa = 0, new_hi = 0, new_si = 0;
     JT        ticks_past; /* avoid div-by-0 by not calling too often :-( */
     char      dummy[16];
     FILE     *stat;

     stat = fopen ("/proc/stat", "r");
     if (!stat)
          return;

     if (fscanf (stat, "%s %lu %lu %lu %lu %lu %lu %lu", dummy,
                 &new_u, &new_n, &new_s, &new_i, &new_wa, &new_hi, &new_si) < 5)
     {
          fclose (stat);
          return;
     }

     fclose (stat);

     ticks_past = ((new_u + new_n + new_s + new_i + new_wa + new_hi + new_si) -
                   (old_u + old_n + old_s + old_i + old_wa + old_hi + old_si));
     if (ticks_past) {
          tmp_u = ((new_u - old_u) << 16) / ticks_past;
          tmp_n = ((new_n - old_n) << 16) / ticks_past;
          tmp_s = ((new_s - old_s) << 16) / ticks_past;
          tmp_i = ((new_i - old_i) << 16) / ticks_past;
     }
     else {
          tmp_u = 0;
          tmp_n = 0;
          tmp_s = 0;
          tmp_i = 0;
     }

     SET_IF_DESIRED(uret, tmp_u);
     SET_IF_DESIRED(nret, tmp_n);
     SET_IF_DESIRED(sret, tmp_s);
     SET_IF_DESIRED(iret, tmp_i);

     old_u  = new_u;
     old_n  = new_n;
     old_s  = new_s;
     old_i  = new_i;
     old_wa = new_wa;
     old_hi = new_hi;
     old_si = new_si;
}
Esempio n. 2
0
static void input_output(double *in, double *out)
{
     static JT      old_in = 0, old_out = 0;
     static double  top_in = 10, top_out = 10;
     double         tmp_in = 0, tmp_out = 0;
     JT             new_in, new_out;
     JT             dummy;
     int            found = 0;
     char           iface[64];
     char           buf[256];
     FILE          *stat;

     stat = fopen ("/proc/net/dev", "r");
     if (!stat)
          return;

     while (fgets (buf, 256, stat)) {
          int i = 0;

          while (buf[i] != 0) {
               if (buf[i] == ':')
                    buf[i] = ' ';

               i++;
          }

          if (sscanf (buf, "%s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu "
                      "%lu %lu %lu %lu\n", iface, &new_in, &dummy, &dummy,
                      &dummy, &dummy, &dummy, &dummy, &dummy, &new_out, &dummy,
                      &dummy, &dummy, &dummy, &dummy, &dummy, &dummy) < 17)
               continue;
          
          if (!strcmp (iface, wanted_iface)) {
               found = 1;
               break;
          }
     }

     fclose (stat);

     if (found) {
          if (old_in) {
               tmp_in = new_in - old_in;

               if (top_in < tmp_in)
                    top_in = tmp_in;

               tmp_in /= top_in;
          }

          if (old_out) {
               tmp_out = new_out - old_out;

               if (top_out < tmp_out)
                    top_out = tmp_out;

               tmp_out /= top_out;
          }

          old_in  = new_in;
          old_out = new_out;
     }

     SET_IF_DESIRED(in, tmp_in);
     SET_IF_DESIRED(out, tmp_out);
}