Advertisement
mtrower

Conky -- Fix CPU reporting on Solaris

Apr 23rd, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 2.31 KB | None | 0 0
  1. From e5b878b2df89df360b809d2cc93c94b15ea61e11 Mon Sep 17 00:00:00 2001
  2. From: "Matthew R. Trower" <dev@blackshard.net>
  3. Date: Tue, 23 Apr 2019 00:15:47 -0500
  4. Subject: [PATCH] Fix CPU reporting on Solaris
  5.  
  6. CPU array should be [ncpu+1], with [0] being average of all CPUs.
  7. ---
  8. src/solaris.cc | 14 +++++++++-----
  9.  1 file changed, 9 insertions(+), 5 deletions(-)
  10.  
  11. diff --git a/src/solaris.cc b/src/solaris.cc
  12. index ae4d3f43..ab447533 100644
  13. --- a/src/solaris.cc
  14. +++ b/src/solaris.cc
  15. @@ -288,6 +288,7 @@ int update_cpu_usage(void) {
  16.    static int last_cpu_cnt = 0;
  17.    static int *last_cpu_use = nullptr;
  18.    double d = current_update_time - last_update_time;
  19. +  double total_cpu_usage = 0;
  20.    int cpu;
  21.  
  22.    if (d < 0.1) return 0;
  23. @@ -298,23 +299,23 @@ int update_cpu_usage(void) {
  24.  
  25.    /* (Re)allocate the array with previous values */
  26.    if (last_cpu_cnt != info.cpu_count || last_cpu_use == nullptr) {
  27. -    last_cpu_use = (int *)realloc(last_cpu_use, info.cpu_count * sizeof(int));
  28. +    last_cpu_use = (int *)realloc(last_cpu_use, (info.cpu_count + 1) * sizeof(int));
  29.      last_cpu_cnt = info.cpu_count;
  30.      if (last_cpu_use == nullptr) return 0;
  31.    }
  32.  
  33. -  info.cpu_usage = (float *)malloc(info.cpu_count * sizeof(float));
  34. +  info.cpu_usage = (float *)malloc((info.cpu_count + 1) * sizeof(float));
  35.  
  36.    pthread_mutex_lock(&kstat_mtx);
  37. -  for (cpu = 0; cpu < info.cpu_count; cpu++) {
  38. +  for (cpu = 1; cpu <= info.cpu_count; cpu++) {
  39.      char stat_name[PATH_MAX];
  40.      unsigned long cpu_user, cpu_nice, cpu_system, cpu_idle;
  41.      unsigned long cpu_use;
  42.      cpu_stat_t *cs;
  43.      kstat_t *ksp;
  44.  
  45. -    snprintf(stat_name, PATH_MAX, "cpu_stat%d", cpu);
  46. -    ksp = kstat_lookup(kstat, (char *)"cpu_stat", cpu, stat_name);
  47. +    snprintf(stat_name, PATH_MAX, "cpu_stat%d", cpu - 1);
  48. +    ksp = kstat_lookup(kstat, (char *)"cpu_stat", cpu - 1, stat_name);
  49.      if (ksp == nullptr) continue;
  50.      if (kstat_read(kstat, ksp, nullptr) == -1) continue;
  51.      cs = (cpu_stat_t *)ksp->ks_data;
  52. @@ -327,10 +328,13 @@ int update_cpu_usage(void) {
  53.      cpu_use = cpu_user + cpu_nice + cpu_system;
  54.  
  55.      info.cpu_usage[cpu] = (double)(cpu_use - last_cpu_use[cpu]) / d / 100.0;
  56. +    total_cpu_usage += info.cpu_usage[cpu];
  57.      last_cpu_use[cpu] = cpu_use;
  58.    }
  59.    pthread_mutex_unlock(&kstat_mtx);
  60.  
  61. +  info.cpu_usage[0] = total_cpu_usage / info.cpu_count;
  62. +
  63.    return 0;
  64.  }
  65.  
  66. --
  67. 2.16.2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement