summaryrefslogtreecommitdiffstats
path: root/src/gallium/auxiliary/hud/hud_diskstat.c
blob: b5d9710ef11e4690b6a8b50bd15489018ac56d58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/**************************************************************************
 *
 * Copyright (C) 2016 Steven Toth <stoth@kernellabs.com>
 * Copyright (C) 2016 Zodiac Inflight Innovations
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/

#ifdef HAVE_GALLIUM_EXTRA_HUD

/* Purpose: Reading /sys/block/<*>/stat MB/s read/write throughput per second,
 * displaying on the HUD.
 */

#include "hud/hud_private.h"
#include "util/list.h"
#include "util/os_time.h"
#include "os/os_thread.h"
#include "util/u_memory.h"
#include "util/u_string.h"
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <stdlib.h>
#include <unistd.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

struct stat_s
{
   /* Read */
   uint64_t r_ios;
   uint64_t r_merges;
   uint64_t r_sectors;
   uint64_t r_ticks;
   /* Write */
   uint64_t w_ios;
   uint64_t w_merges;
   uint64_t w_sectors;
   uint64_t w_ticks;
   /* Misc */
   uint64_t in_flight;
   uint64_t io_ticks;
   uint64_t time_in_queue;
};

struct diskstat_info
{
   struct list_head list;
   int mode; /* DISKSTAT_RD, DISKSTAT_WR */
   char name[64]; /* EG. sda5 */

   char sysfs_filename[128];
   uint64_t last_time;
   struct stat_s last_stat;
};

/* TODO: We don't handle dynamic block device / partition
 * arrival or removal.
 * Static globals specific to this HUD category.
 */
static int gdiskstat_count = 0;
static struct list_head gdiskstat_list;
static mtx_t gdiskstat_mutex = _MTX_INITIALIZER_NP;

static struct diskstat_info *
find_dsi_by_name(const char *n, int mode)
{
   list_for_each_entry(struct diskstat_info, dsi, &gdiskstat_list, list) {
      if (dsi->mode != mode)
         continue;
      if (strcasecmp(dsi->name, n) == 0)
         return dsi;
   }
   return 0;
}

static int
get_file_values(const char *fn, struct stat_s *s)
{
   int ret = 0;
   FILE *fh = fopen(fn, "r");
   if (!fh)
      return -1;

   ret = fscanf(fh,
        "%" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64
        " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 "",
        &s->r_ios, &s->r_merges, &s->r_sectors, &s->r_ticks, &s->w_ios,
        &s->w_merges, &s->w_sectors, &s->w_ticks, &s->in_flight, &s->io_ticks,
        &s->time_in_queue);

   fclose(fh);

   return ret;
}

static void
query_dsi_load(struct hud_graph *gr, struct pipe_context *pipe)
{
   /* The framework calls us periodically, compensate for the
    * calling interval accordingly when reporting per second.
    */
   struct diskstat_info *dsi = gr->query_data;
   uint64_t now = os_time_get();

   if (dsi->last_time) {
      if (dsi->last_time + gr->pane->period <= now) {
         struct stat_s stat;
         if (get_file_values(dsi->sysfs_filename, &stat) < 0)
            return;
         float val = 0;

         switch (dsi->mode) {
         case DISKSTAT_RD:
            val =
               ((stat.r_sectors -
                 dsi->last_stat.r_sectors) * 512) /
               (((float) gr->pane->period / 1000) / 1000);
            break;
         case DISKSTAT_WR:
            val =
               ((stat.w_sectors -
                 dsi->last_stat.w_sectors) * 512) /
               (((float) gr->pane->period / 1000) / 1000);
            break;
         }

         hud_graph_add_value(gr, (uint64_t) val);
         dsi->last_stat = stat;
         dsi->last_time = now;
      }
   }
   else {
      /* initialize */
      switch (dsi->mode) {
      case DISKSTAT_RD:
      case DISKSTAT_WR:
         get_file_values(dsi->sysfs_filename, &dsi->last_stat);
         break;
      }
      dsi->last_time = now;
   }
}

/**
  * Create and initialize a new object for a specific block I/O device.
  * \param  pane  parent context.
  * \param  dev_name  logical block device name, EG. sda5.
  * \param  mode  query read or write (DISKSTAT_RD/DISKSTAT_WR) statistics.
  */
void
hud_diskstat_graph_install(struct hud_pane *pane, const char *dev_name,
                           unsigned int mode)
{
   struct hud_graph *gr;
   struct diskstat_info *dsi;

   int num_devs = hud_get_num_disks(0);
   if (num_devs <= 0)
      return;

   dsi = find_dsi_by_name(dev_name, mode);
   if (!dsi)
      return;

   gr = CALLOC_STRUCT(hud_graph);
   if (!gr)
      return;

   dsi->mode = mode;
   if (dsi->mode == DISKSTAT_RD) {
      snprintf(gr->name, sizeof(gr->name), "%s-Read-MB/s", dsi->name);
   }
   else if (dsi->mode == DISKSTAT_WR) {
      snprintf(gr->name, sizeof(gr->name), "%s-Write-MB/s", dsi->name);
   }
   else {
      free(gr);
      return;
   }

   gr->query_data = dsi;
   gr->query_new_value = query_dsi_load;

   hud_pane_add_graph(pane, gr);
   hud_pane_set_max_value(pane, 100);
}

static void
add_object_part(const char *basename, const char *name, int objmode)
{
   struct diskstat_info *dsi = CALLOC_STRUCT(diskstat_info);

   snprintf(dsi->name, sizeof(dsi->name), "%s", name);
   snprintf(dsi->sysfs_filename, sizeof(dsi->sysfs_filename), "%s/%s/stat",
      basename, name);
   dsi->mode = objmode;
   list_addtail(&dsi->list, &gdiskstat_list);
   gdiskstat_count++;
}

static void
add_object(const char *basename, const char *name, int objmode)
{
   struct diskstat_info *dsi = CALLOC_STRUCT(diskstat_info);

   snprintf(dsi->name, sizeof(dsi->name), "%s", name);
   snprintf(dsi->sysfs_filename, sizeof(dsi->sysfs_filename), "%s/stat",
      basename);
   dsi->mode = objmode;
   list_addtail(&dsi->list, &gdiskstat_list);
   gdiskstat_count++;
}

/**
  * Initialize internal object arrays and display block I/O HUD help.
  * \param  displayhelp  true if the list of detected devices should be
                         displayed on the console.
  * \return  number of detected block I/O devices.
  */
int
hud_get_num_disks(bool displayhelp)
{
   struct dirent *dp;
   struct stat stat_buf;
   char name[64];

   /* Return the number of block devices and partitions. */
   mtx_lock(&gdiskstat_mutex);
   if (gdiskstat_count) {
      mtx_unlock(&gdiskstat_mutex);
      return gdiskstat_count;
   }

   /* Scan /sys/block, for every object type we support, create and
    * persist an object to represent its different statistics.
    */
   list_inithead(&gdiskstat_list);
   DIR *dir = opendir("/sys/block/");
   if (!dir) {
      mtx_unlock(&gdiskstat_mutex);
      return 0;
   }

   while ((dp = readdir(dir)) != NULL) {

      /* Avoid 'lo' and '..' and '.' */
      if (strlen(dp->d_name) <= 2)
         continue;

      char basename[256];
      snprintf(basename, sizeof(basename), "/sys/block/%s", dp->d_name);
      snprintf(name, sizeof(name), "%s/stat", basename);
      if (stat(name, &stat_buf) < 0)
         continue;

      if (!S_ISREG(stat_buf.st_mode))
         continue;              /* Not a regular file */

      /* Add a physical block device with R/W stats */
      add_object(basename, dp->d_name, DISKSTAT_RD);
      add_object(basename, dp->d_name, DISKSTAT_WR);

      /* Add any partitions */
      struct dirent *dpart;
      DIR *pdir = opendir(basename);
      if (!pdir) {
         mtx_unlock(&gdiskstat_mutex);
         closedir(dir);
         return 0;
      }

      while ((dpart = readdir(pdir)) != NULL) {
         /* Avoid 'lo' and '..' and '.' */
         if (strlen(dpart->d_name) <= 2)
            continue;

         char p[64];
         snprintf(p, sizeof(p), "%s/%s/stat", basename, dpart->d_name);
         if (stat(p, &stat_buf) < 0)
            continue;

         if (!S_ISREG(stat_buf.st_mode))
            continue;           /* Not a regular file */

         /* Add a partition with R/W stats */
         add_object_part(basename, dpart->d_name, DISKSTAT_RD);
         add_object_part(basename, dpart->d_name, DISKSTAT_WR);
      }
   }
   closedir(dir);

   if (displayhelp) {
      list_for_each_entry(struct diskstat_info, dsi, &gdiskstat_list, list) {
         char line[32];
         snprintf(line, sizeof(line), "    diskstat-%s-%s",
                 dsi->mode == DISKSTAT_RD ? "rd" :
                 dsi->mode == DISKSTAT_WR ? "wr" : "undefined", dsi->name);

         puts(line);
      }
   }
   mtx_unlock(&gdiskstat_mutex);

   return gdiskstat_count;
}

#endif /* HAVE_GALLIUM_EXTRA_HUD */