Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /**************************************************************************
  2.  *
  3.  * Copyright 2013 Marek Olšák <maraeo@gmail.com>
  4.  * All Rights Reserved.
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a
  7.  * copy of this software and associated documentation files (the
  8.  * "Software"), to deal in the Software without restriction, including
  9.  * without limitation the rights to use, copy, modify, merge, publish,
  10.  * distribute, sub license, and/or sell copies of the Software, and to
  11.  * permit persons to whom the Software is furnished to do so, subject to
  12.  * the following conditions:
  13.  *
  14.  * The above copyright notice and this permission notice (including the
  15.  * next paragraph) shall be included in all copies or substantial portions
  16.  * of the Software.
  17.  *
  18.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  21.  * IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
  22.  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  23.  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  24.  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25.  *
  26.  **************************************************************************/
  27.  
  28. /* This file contains code for reading CPU load for displaying on the HUD.
  29.  */
  30.  
  31. #include "hud/hud_private.h"
  32. #include "os/os_time.h"
  33. #include "util/u_memory.h"
  34. #include <stdio.h>
  35. #include <inttypes.h>
  36.  
  37. static boolean
  38. get_cpu_stats(unsigned cpu_index, uint64_t *busy_time, uint64_t *total_time)
  39. {
  40.    char cpuname[32];
  41.    char line[1024];
  42.    FILE *f;
  43.  
  44.    if (cpu_index == ALL_CPUS)
  45.       strcpy(cpuname, "cpu");
  46.    else
  47.       sprintf(cpuname, "cpu%u", cpu_index);
  48.  
  49.    f = fopen("/proc/stat", "r");
  50.    if (!f)
  51.       return FALSE;
  52.  
  53.    while (!feof(f) && fgets(line, sizeof(line), f)) {
  54.       if (strstr(line, cpuname) == line) {
  55.          uint64_t v[12];
  56.          int i, num;
  57.  
  58.          num = sscanf(line,
  59.                       "%s %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64
  60.                       " %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64
  61.                       " %"PRIu64" %"PRIu64"",
  62.                       cpuname, &v[0], &v[1], &v[2], &v[3], &v[4], &v[5],
  63.                       &v[6], &v[7], &v[8], &v[9], &v[10], &v[11]);
  64.          if (num < 5) {
  65.             fclose(f);
  66.             return FALSE;
  67.          }
  68.  
  69.          /* user + nice + system */
  70.          *busy_time = v[0] + v[1] + v[2];
  71.          *total_time = *busy_time;
  72.  
  73.          /* ... + idle + iowait + irq + softirq + ...  */
  74.          for (i = 3; i < num-1; i++) {
  75.             *total_time += v[i];
  76.          }
  77.          fclose(f);
  78.          return TRUE;
  79.       }
  80.    }
  81.    fclose(f);
  82.    return FALSE;
  83. }
  84.  
  85. struct cpu_info {
  86.    unsigned cpu_index;
  87.    uint64_t last_cpu_busy, last_cpu_total, last_time;
  88. };
  89.  
  90. static void
  91. query_cpu_load(struct hud_graph *gr)
  92. {
  93.    struct cpu_info *info = gr->query_data;
  94.    uint64_t now = os_time_get();
  95.  
  96.    if (info->last_time) {
  97.       if (info->last_time + gr->pane->period <= now) {
  98.          uint64_t cpu_busy, cpu_total, cpu_load;
  99.  
  100.          get_cpu_stats(info->cpu_index, &cpu_busy, &cpu_total);
  101.  
  102.          cpu_load = (cpu_busy - info->last_cpu_busy) * 100 /
  103.                     (double)(cpu_total - info->last_cpu_total);
  104.          hud_graph_add_value(gr, cpu_load);
  105.  
  106.          info->last_cpu_busy = cpu_busy;
  107.          info->last_cpu_total = cpu_total;
  108.          info->last_time = now;
  109.       }
  110.    }
  111.    else {
  112.       /* initialize */
  113.       info->last_time = now;
  114.       get_cpu_stats(info->cpu_index, &info->last_cpu_busy,
  115.                     &info->last_cpu_total);
  116.    }
  117. }
  118.  
  119. static void
  120. free_query_data(void *p)
  121. {
  122.    FREE(p);
  123. }
  124.  
  125. void
  126. hud_cpu_graph_install(struct hud_pane *pane, unsigned cpu_index)
  127. {
  128.    struct hud_graph *gr;
  129.    struct cpu_info *info;
  130.    uint64_t busy, total;
  131.  
  132.    /* see if the cpu exists */
  133.    if (cpu_index != ALL_CPUS && !get_cpu_stats(cpu_index, &busy, &total)) {
  134.       return;
  135.    }
  136.  
  137.    gr = CALLOC_STRUCT(hud_graph);
  138.    if (!gr)
  139.       return;
  140.  
  141.    if (cpu_index == ALL_CPUS)
  142.       strcpy(gr->name, "cpu");
  143.    else
  144.       sprintf(gr->name, "cpu%u", cpu_index);
  145.  
  146.    gr->query_data = CALLOC_STRUCT(cpu_info);
  147.    if (!gr->query_data) {
  148.       FREE(gr);
  149.       return;
  150.    }
  151.  
  152.    gr->query_new_value = query_cpu_load;
  153.  
  154.    /* Don't use free() as our callback as that messes up Gallium's
  155.     * memory debugger.  Use simple free_query_data() wrapper.
  156.     */
  157.    gr->free_query_data = free_query_data;
  158.  
  159.    info = gr->query_data;
  160.    info->cpu_index = cpu_index;
  161.  
  162.    hud_pane_add_graph(pane, gr);
  163.    hud_pane_set_max_value(pane, 100);
  164. }
  165.  
  166. int
  167. hud_get_num_cpus(void)
  168. {
  169.    uint64_t busy, total;
  170.    int i = 0;
  171.  
  172.    while (get_cpu_stats(i, &busy, &total))
  173.       i++;
  174.  
  175.    return i;
  176. }
  177.