]> xenbits.xensource.com Git - xentesttools/bootstrap.git/commitdiff
Updated to also check the network (from df_netload.c)
authorKonrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Wed, 9 Feb 2011 16:34:53 +0000 (11:34 -0500)
committerKonrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Wed, 9 Feb 2011 16:34:53 +0000 (11:34 -0500)
root_image/tools/drfb_load/df_cpuload.c

index 45bfdeb36d3031db17623cee0dfa8859bfc133ae..0928bf9e5b309aec407d8ba9be70a8e226355321 100644 (file)
 
 static IDirectFB             *dfb     = NULL;
 static IDirectFBDisplayLayer *layer   = NULL;
-static IDirectFBWindow       *window  = NULL;
-static IDirectFBSurface      *surface = NULL;
-static IDirectFBEventBuffer  *events  = NULL;
-
+static IDirectFBWindow       *window_cpu  = NULL;
+static IDirectFBWindow       *window_net  = NULL;
+static IDirectFBSurface      *surface_cpu = NULL;
+static IDirectFBSurface      *surface_net = NULL;
+static IDirectFBEventBuffer  *events_cpu  = NULL;
+static int def_width = 0;
 /******************************************************************************/
 
 static void init_application( int *argc, char **argv[] );
 static void exit_application( int status );
 
-static void update();
+static void update_cpu();
+static void update_net();
 
 /******************************************************************************/
 
@@ -59,20 +62,23 @@ main( int argc, char *argv[] )
      /* Initialize application. */
      init_application( &argc, &argv );
 
-     surface->Clear( surface, 0xff, 0xff, 0xff, 0x30 );
+     surface_cpu->Clear( surface_cpu, 0xff, 0xff, 0xff, 0x30 );
+     surface_net->Clear( surface_net, 0xff, 0xff, 0xff, 0x30 );
 
-     window->SetOpacity( window, 0xff );
+     window_cpu->SetOpacity( window_cpu, 0xff );
+     window_net->SetOpacity( window_net, 0xff );
 
      /* Main loop. */
      while (1) {
           DFBWindowEvent event;
 
-          update();
+         update_cpu();
+          update_net();
 
-          events->WaitForEventWithTimeout( events, 0, 100 );
+          events_cpu->WaitForEventWithTimeout( events_cpu, 0, 100 );
 
-          /* Check for new events. */
-          while (events->GetEvent( events, DFB_EVENT(&event) ) == DFB_OK) {
+          /* Check for new events_cpu. */
+          while (events_cpu->GetEvent( events_cpu, DFB_EVENT(&event) ) == DFB_OK) {
                switch (event.type) {
                     default:
                          break;
@@ -114,63 +120,92 @@ init_application( int *argc, char **argv[] )
           exit_application( 3 );
      }
 
      /* Get the screen size etc. */
      layer->GetConfiguration( layer, &config );
 
-     /* Fill the window description. */
+     /* Fill the window_cpu description. */
      desc.flags  = DWDESC_POSX | DWDESC_POSY |
                    DWDESC_WIDTH | DWDESC_HEIGHT | DWDESC_CAPS;
-     desc.posx   = config.width - 224;
+     def_width   = config.width-8; 
+     desc.posx   = 8;
      desc.posy   = 8;
-     desc.width  = 64;
-     desc.height = 64;
+     desc.width  = def_width;
+     desc.height = 127;
      desc.caps   = DWCAPS_ALPHACHANNEL | DWCAPS_NODECORATION;
 
-     /* Create the window. */
-     ret = layer->CreateWindow( layer, &desc, &window );
+     /* Create the window_cpu. */
+     ret = layer->CreateWindow( layer, &desc, &window_cpu );
      if (ret) {
           DirectFBError( "IDirectFBDisplayLayer::CreateWindow() failed", ret );
           exit_application( 4 );
      }
 
-     /* Get the window's surface. */
-     ret = window->GetSurface( window, &surface );
+     /* Create the window_net. */
+     desc.posy += 127;
+     ret = layer->CreateWindow( layer, &desc, &window_net );
+     if (ret) {
+          DirectFBError( "IDirectFBDisplayLayer::CreateWindow() failed", ret );
+          exit_application( 4 );
+     }
+     ret = window_cpu->GetSurface( window_cpu, &surface_cpu );
+     if (ret) {
+          DirectFBError( "IDirectFBWindow::GetSurface() failed", ret );
+          exit_application( 5 );
+     }
+     /* Get the window_cpu's surface_cpu. */
+     ret = window_cpu->GetSurface( window_net, &surface_net );
      if (ret) {
           DirectFBError( "IDirectFBWindow::GetSurface() failed", ret );
           exit_application( 5 );
      }
 
-     /* Create an event buffer for all keyboard events. */
-     ret = window->CreateEventBuffer( window, &events );
+     /* Create an event buffer for all keyboard events_cpu. */
+     ret = window_cpu->CreateEventBuffer( window_cpu, &events_cpu );
      if (ret) {
           DirectFBError( "IDirectFBWindow::CreateEventBuffer() failed", ret );
           exit_application( 6 );
      }
+     /* Add ghost option (behave like an overlay). */
+     window_cpu->SetOptions( window_cpu, DWOP_ALPHACHANNEL | DWOP_GHOST );
+
+     /* Move window_cpu to upper stacking class. */
+     window_cpu->SetStackingClass( window_cpu, DWSC_UPPER );
+
+     /* Make it the top most window_cpu. */
+     window_cpu->RaiseToTop( window_cpu );
 
      /* Add ghost option (behave like an overlay). */
-     window->SetOptions( window, DWOP_ALPHACHANNEL | DWOP_GHOST );
+     window_net->SetOptions( window_net, DWOP_ALPHACHANNEL | DWOP_GHOST );
 
-     /* Move window to upper stacking class. */
-     window->SetStackingClass( window, DWSC_UPPER );
 
-     /* Make it the top most window. */
-     window->RaiseToTop( window );
+     /* Move window_net to upper stacking class. */
+     //window_net->SetStackingClass( window_net, DWSC_UPPER );
+
+     /* Make it the top most window_net. */
+     //window_net->RaiseToTop( window_net );
 }
 
 static void
 exit_application( int status )
 {
      /* Release the event buffer. */
-     if (events)
-          events->Release( events );
+     if (events_cpu)
+          events_cpu->Release( events_cpu );
+
+     /* Release the window_cpu's surface_cpu. */
+     if (surface_cpu)
+          surface_cpu->Release( surface_cpu );
+     if (surface_net)
+          surface_net->Release( surface_net );
 
-     /* Release the window's surface. */
-     if (surface)
-          surface->Release( surface );
+     /* Release the window_cpu. */
+     if (window_cpu)
+          window_cpu->Release( window_cpu );
 
-     /* Release the window. */
-     if (window)
-          window->Release( window );
+     if (window_net)
+          window_net->Release( window_net );
 
      /* Release the layer. */
      if (layer)
@@ -252,27 +287,150 @@ get_load()
      load = u + n + s;
 
      old_load = (load + load + load + old_load) >> 2;
-
-     return old_load >> 10;
+     return old_load >> 9;
 }
 
 static void
-update()
+update_cpu()
 {
      int load = get_load();
 
-     surface->SetColor( surface, 0xff, 0xff, 0xff, 0x30 );
-     surface->FillRectangle( surface, 63, 0, 1, 64 - load );
+       /* Black? */
+     surface_cpu->SetColor( surface_cpu, 0xff, 0xff, 0xff, 0x30 );
+     surface_cpu->FillRectangle( surface_cpu, def_width, 0, 1, 127 - load );
+
+       /* load colors */
+     surface_cpu->SetColor( surface_cpu, 0x00, 0x50, 0xd0, 0xcc );
+     surface_cpu->FillRectangle( surface_cpu, def_width-1, 127 - load, 1, load );
 
-     surface->SetColor( surface, 0x00, 0x50, 0xd0, 0xcc );
-     surface->FillRectangle( surface, 63, 64 - load, 1, load );
+     surface_cpu->Blit( surface_cpu, surface_cpu, NULL, -1, 0 );
+
+       /* White? */
+     surface_cpu->SetColor( surface_cpu, 0x00, 0x00, 0x00, 0x60 );
+     surface_cpu->DrawRectangle( surface_cpu, 0, 0, def_width /* width */, 127 );
+
+
+     surface_cpu->Flip( surface_cpu, NULL, 0 );
+}
+char *wanted_iface="wlan1";
+/******************************************************************************/
+
+#define SET_IF_DESIRED(x,y) do{  if(x) *(x) = (y); }while(0)
+#define JT unsigned long
+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);
+}
+#undef JT
+
+static void
+get_net_load(int mult, int *ret_r_total, int *ret_t_total)
+{
+     static double old_r_total = 0;
+     static double old_t_total = 0;
+
+     double r_total = 0, t_total = 0;
+
+     input_output( &r_total, &t_total );
+
+     *ret_r_total = ((r_total + old_r_total + old_r_total + old_r_total) / 3.0)*mult;
+     *ret_t_total = ((t_total + old_t_total + old_t_total + old_t_total) / 3.0)*mult;
+
+     old_r_total = r_total;
+     old_t_total = t_total;
+}
+
+static void
+update_net()
+{
+     int rx_load, tx_load;
+
+     get_net_load(127, &rx_load,&tx_load);
+
+     surface_net->SetColor( surface_net, 0xff, 0xff, 0xff, 0x30 );
+
+     if (rx_load > tx_load) {
+       surface_net->FillRectangle( surface_net, def_width, 0, 1, 127 - rx_load );
+       surface_net->SetColor( surface_net, 0x00, 0xA0, 0x00, 0xcc );
+       surface_net->FillRectangle( surface_net, def_width-1, 127 - rx_load, 1, rx_load );
+       surface_net->SetColor( surface_net, 0x00, 0xF0, 0x00, 0xcc );
+       surface_net->FillRectangle( surface_net, def_width-1, 127 - tx_load, 1, tx_load );
+     } else {
+       surface_net->FillRectangle( surface_net, def_width-1, 0, 1, 127 - tx_load );
+       surface_net->SetColor( surface_net, 0x00, 0xF0, 0x00, 0xcc );
+       surface_net->FillRectangle( surface_net, def_width-1, 127 - tx_load, 1, tx_load );
+       surface_net->SetColor( surface_net, 0x00, 0xA0, 0x00, 0xcc );
+       surface_net->FillRectangle( surface_net, def_width-1, 127 - rx_load, 1, rx_load );
+     }
 
-     surface->Blit( surface, surface, NULL, -1, 0 );
+     surface_net->Blit( surface_net, surface_net, NULL, -1, 0 );
 
-     surface->SetColor( surface, 0x00, 0x00, 0x00, 0x60 );
-     surface->DrawRectangle( surface, 0, 0, 64, 64 );
+     surface_net->SetColor( surface_net, 0x00, 0x00, 0x00, 0x60 );
+     surface_net->DrawRectangle( surface_net, 0, 0, def_width, 127 );
 
 
-     surface->Flip( surface, NULL, 0 );
+     surface_net->Flip( surface_net, NULL, 0 );
 }