| 1 | #include <config.h> |
| 2 | |
| 3 | #include <math.h> |
| 4 | |
| 5 | #include <glib/gi18n.h> |
| 6 | |
| 7 | #include <glibtop.h> |
| 8 | #include <glibtop/cpu.h> |
| 9 | #include <glibtop/disk.h> |
| 10 | #include <glibtop/mem.h> |
| 11 | #include <glibtop/swap.h> |
| 12 | #include <glibtop/netload.h> |
| 13 | #include <glibtop/netlist.h> |
| 14 | |
| 15 | #include "application.h" |
| 16 | #include "load-graph.h" |
| 17 | #include "util.h" |
| 18 | #include "legacy/gsm_color_button.h" |
| 19 | |
| 20 | constexpr double BORDER_ALPHA = 0.7; |
| 21 | constexpr double GRID_ALPHA = BORDER_ALPHA / 2.0; |
| 22 | constexpr int FRAME_WIDTH = 4; |
| 23 | constexpr unsigned GRAPH_MIN_HEIGHT = 40; |
| 24 | |
| 25 | void |
| 26 | LoadGraph::clear_background () |
| 27 | { |
| 28 | gsm_graph_clear_background (GSM_GRAPH (disp)); |
| 29 | } |
| 30 | |
| 31 | bool |
| 32 | LoadGraph::is_logarithmic_scale () const |
| 33 | { |
| 34 | // logarithmic scale is used only for memory graph |
| 35 | return this->type == LOAD_GRAPH_MEM && GsmApplication::get ().config.logarithmic_scale; |
| 36 | } |
| 37 | |
| 38 | /* |
| 39 | Returns Y scale caption based on give index of the label. |
| 40 | Takes into account whether the scale should be logarithmic for memory graph. |
| 41 | */ |
| 42 | char* |
| 43 | LoadGraph::get_caption (guint index) |
| 44 | { |
| 45 | char *caption; |
| 46 | guint64 max_value; |
| 47 | |
| 48 | if (this->type == LOAD_GRAPH_NET) |
| 49 | max_value = this->net.max; |
| 50 | else if (this->type == LOAD_GRAPH_DISK) |
| 51 | max_value = this->disk.max; |
| 52 | else |
| 53 | max_value = 100; |
| 54 | |
| 55 | // operation orders matters so it's 0 if index == num_bars |
| 56 | float caption_percentage = (float)max_value - index * (float)max_value / this->num_bars; |
| 57 | |
| 58 | if (this->is_logarithmic_scale ()) |
| 59 | { |
| 60 | float caption_value = caption_percentage == 0 ? 0 : pow (100, caption_percentage / max_value); |
| 61 | // Translators: loadgraphs y axis percentage labels: 0 %, 50%, 100% |
| 62 | caption = g_strdup_printf (_("%.0f %%"), caption_value); |
| 63 | } |
| 64 | else if (this->type == LOAD_GRAPH_NET) |
| 65 | { |
| 66 | const std::string captionstr (procman::format_network_rate ((guint64)caption_percentage)); |
| 67 | caption = g_strdup (captionstr.c_str ()); |
| 68 | } |
| 69 | else if (this->type == LOAD_GRAPH_DISK) |
| 70 | { |
| 71 | const std::string captionstr (procman::format_rate ((guint64)caption_percentage)); |
| 72 | caption = g_strdup (captionstr.c_str ()); |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | // Translators: loadgraphs y axis percentage labels: 0 %, 50%, 100% |
| 77 | caption = g_strdup_printf (_("%.0f %%"), caption_percentage); |
| 78 | } |
| 79 | |
| 80 | return caption; |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | Translates y partial position to logarithmic position if set to logarithmic scale. |
| 85 | */ |
| 86 | float |
| 87 | LoadGraph::translate_to_log_partial_if_needed (float position_partial) |
| 88 | { |
| 89 | if (this->is_logarithmic_scale ()) |
| 90 | position_partial = position_partial == 0 ? 0 : log10 (position_partial * 100) / 2; |
| 91 | |
| 92 | return position_partial; |
| 93 | } |
| 94 | |
| 95 | static gchar* |
| 96 | format_duration (unsigned seconds) |
| 97 | { |
| 98 | gchar *caption = NULL; |
| 99 | |
| 100 | unsigned minutes = seconds / 60; |
| 101 | unsigned hours = seconds / 3600; |
| 102 | |
| 103 | if (hours != 0) |
| 104 | { |
| 105 | if (minutes % 60 == 0) |
| 106 | { |
| 107 | // If minutes mod 60 is 0 set it to 0, to prevent it from showing full hours in |
| 108 | // minutes in addition to hours. |
| 109 | minutes = 0; |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | // Round minutes as seconds wont get shown if neither hours nor minutes are 0. |
| 114 | minutes = int(rint (seconds / 60.0)) % 60; |
| 115 | if (minutes == 0) |
| 116 | { |
| 117 | // Increase hours if rounding minutes results in 0, because that would be |
| 118 | // what it would be rounded to. |
| 119 | hours++; |
| 120 | // Set seconds to hours * 3600 to prevent seconds from being drawn. |
| 121 | seconds = hours * 3600; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | gchar*captionH = g_strdup_printf (dngettext (GETTEXT_PACKAGE, "%u hr", "%u hrs", hours), hours); |
| 127 | gchar*captionM = g_strdup_printf (dngettext (GETTEXT_PACKAGE, "%u min", "%u mins", minutes), |
| 128 | minutes); |
| 129 | gchar*captionS = g_strdup_printf (dngettext (GETTEXT_PACKAGE, "%u sec", "%u secs", seconds % 60), |
| 130 | seconds % 60); |
| 131 | |
| 132 | caption = g_strjoin (" ", hours > 0 ? captionH : "", |
| 133 | minutes > 0 ? captionM : "", |
| 134 | seconds % 60 > 0 ? captionS : "", |
| 135 | NULL); |
| 136 | g_free (captionH); |
| 137 | g_free (captionM); |
| 138 | g_free (captionS); |
| 139 | |
| 140 | return caption; |
| 141 | } |
| 142 | |
| 143 | static void |
| 144 | load_graph_rescale (LoadGraph *graph) |
| 145 | { |
| 146 | ///org/gnome/desktop/interface/text-scaling-factor |
| 147 | gsm_graph_set_font_size (GSM_GRAPH (graph->disp), 8 * graph->font_settings->get_double ("text-scaling-factor")); |
| 148 | } |
| 149 | |
| 150 | static cairo_surface_t* |
| 151 | create_background (LoadGraph *graph, |
| 152 | int width, |
| 153 | int height) |
| 154 | { |
| 155 | GdkRGBA fg_color; |
| 156 | GdkRGBA grid_color; |
| 157 | PangoContext *pango_context; |
| 158 | PangoFontDescription *font_desc; |
| 159 | PangoLayout *layout; |
| 160 | cairo_t *cr; |
| 161 | cairo_surface_t *surface; |
| 162 | double fontsize = gsm_graph_get_font_size (graph->disp); |
| 163 | double rmargin = gsm_graph_get_right_margin (graph->disp); |
| 164 | guint num_bars = gsm_graph_get_num_bars (graph->disp, height + (2 * FRAME_WIDTH)); |
| 165 | const guint num_sections = 7; |
| 166 | |
| 167 | guint indent = gsm_graph_get_indent (graph->disp); |
| 168 | |
| 169 | /* Graph length */ |
| 170 | const unsigned total_seconds = graph->speed * (graph->num_points - 2) / 1000; |
| 171 | guint scale = gtk_widget_get_scale_factor (GTK_WIDGET (graph->disp)); |
| 172 | |
| 173 | surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, |
| 174 | gtk_widget_get_width (GTK_WIDGET (graph->disp)) * scale, |
| 175 | gtk_widget_get_height (GTK_WIDGET (graph->disp)) * scale); |
| 176 | cairo_surface_set_device_scale (surface, scale, scale); |
| 177 | cr = cairo_create (surface); |
| 178 | |
| 179 | /* Create grid label layout */ |
| 180 | layout = pango_cairo_create_layout (cr); |
| 181 | |
| 182 | /* Set font for graph labels */ |
| 183 | pango_context = gtk_widget_get_pango_context (GTK_WIDGET (graph->disp)); |
| 184 | font_desc = pango_context_get_font_description (pango_context); |
| 185 | pango_font_description_set_size (font_desc, 0.8 * fontsize * PANGO_SCALE); |
| 186 | pango_layout_set_font_description (layout, font_desc); |
| 187 | |
| 188 | /* draw frame */ |
| 189 | cairo_translate (cr, FRAME_WIDTH, FRAME_WIDTH); |
| 190 | |
| 191 | /* Draw background rectangle */ |
| 192 | /* When a user uses a dark theme, the hard-coded |
| 193 | * white background in GSM is a lone white on the |
| 194 | * display, which makes the user unhappy. To fix |
| 195 | * this, here we offer the user a chance to set |
| 196 | * his favorite background color. */ |
| 197 | GtkStyleContext *context = gtk_widget_get_style_context (GTK_WIDGET (graph->disp)); |
| 198 | |
| 199 | gtk_style_context_save (context); |
| 200 | |
| 201 | /* Here we specify the name of the class. Now in |
| 202 | * the theme's CSS we can specify the own colors |
| 203 | * for this class. */ |
| 204 | gtk_style_context_add_class (context, "loadgraph"); |
| 205 | |
| 206 | /* Get foreground color */ |
| 207 | gtk_style_context_get_color (context, &fg_color); |
| 208 | /* The grid color is the same as the foreground color but has sometimes |
| 209 | * different alpha. Keep it separate. */ |
| 210 | grid_color = fg_color; |
| 211 | |
| 212 | /* Why not use the new features of the |
| 213 | * GTK instead of cairo_rectangle ?! :) */ |
| 214 | gtk_render_background (context, cr, indent, 0.0, |
| 215 | width - rmargin - indent, |
| 216 | graph->real_draw_height); |
| 217 | |
| 218 | gtk_style_context_restore (context); |
| 219 | |
| 220 | cairo_set_line_width (cr, 0.25); |
| 221 | |
| 222 | /* Horizontal grid lines */ |
| 223 | for (guint i = 0; i <= num_bars; i++) |
| 224 | { |
| 225 | PangoRectangle extents; |
| 226 | |
| 227 | /* Label alignment */ |
| 228 | double y; |
| 229 | if (i == 0) |
| 230 | /* Below the line */ |
| 231 | y = 0.5 + fontsize / 2.0; |
| 232 | else if (i == num_bars) |
| 233 | /* Above the line */ |
| 234 | y = i * graph->graph_dely + 0.5; |
| 235 | else |
| 236 | /* Next to the line */ |
| 237 | y = i * graph->graph_dely + fontsize / 2.0; |
| 238 | |
| 239 | /* Draw the label */ |
| 240 | /* Prepare the text */ |
| 241 | gchar *caption = graph->get_caption (i); |
| 242 | pango_layout_set_text (layout, caption, -1); |
| 243 | pango_layout_set_alignment (layout, PANGO_ALIGN_LEFT); |
| 244 | pango_layout_get_extents (layout, NULL, &extents); |
| 245 | |
| 246 | /* Create y axis position modifier */ |
| 247 | double label_y_offset_modifier = i == 0 ? 0.5 |
| 248 | : i == num_bars |
| 249 | ? 1.0 |
| 250 | : 0.85; |
| 251 | |
| 252 | /* Set the label position */ |
| 253 | cairo_move_to (cr, |
| 254 | width - indent - 23, |
| 255 | y - label_y_offset_modifier * extents.height / PANGO_SCALE); |
| 256 | |
| 257 | /* Set the color */ |
| 258 | gdk_cairo_set_source_rgba (cr, &fg_color); |
| 259 | |
| 260 | /* Paint the grid label */ |
| 261 | pango_cairo_show_layout (cr, layout); |
| 262 | g_free (caption); |
| 263 | |
| 264 | /* Set the grid line alpha */ |
| 265 | if (i == 0 || i == num_bars) |
| 266 | grid_color.alpha = BORDER_ALPHA; |
| 267 | else |
| 268 | grid_color.alpha = GRID_ALPHA; |
| 269 | |
| 270 | /* Draw the line */ |
| 271 | /* Set the color */ |
| 272 | gdk_cairo_set_source_rgba (cr, &grid_color); |
| 273 | |
| 274 | /* Set the grid line path */ |
| 275 | cairo_move_to (cr, indent, i * graph->graph_dely); |
| 276 | cairo_line_to (cr, |
| 277 | width - rmargin + 4, |
| 278 | i * graph->graph_dely); |
| 279 | } |
| 280 | |
| 281 | /* Vertical grid lines */ |
| 282 | for (unsigned int i = 0; i < num_sections; i++) |
| 283 | { |
| 284 | PangoRectangle extents; |
| 285 | |
| 286 | /* Prepare the x position */ |
| 287 | double x = ceil (i * (width - rmargin - indent) / (num_sections - 1)); |
| 288 | |
| 289 | /* Draw the label */ |
| 290 | /* Prepare the text */ |
| 291 | gchar *caption = format_duration (total_seconds - i * total_seconds / (num_sections - 1)); |
| 292 | pango_layout_set_text (layout, caption, -1); |
| 293 | pango_layout_get_extents (layout, NULL, &extents); |
| 294 | |
| 295 | /* Create x axis position modifier */ |
| 296 | double label_x_offset_modifier = i == 0 ? 0 |
| 297 | : i == (num_sections - 1) |
| 298 | ? 1.0 |
| 299 | : 0.5; |
| 300 | |
| 301 | /* Set the label position */ |
| 302 | cairo_move_to (cr, |
| 303 | x + indent - label_x_offset_modifier * extents.width / PANGO_SCALE + 1.0, |
| 304 | height - 1.0 * extents.height / PANGO_SCALE); |
| 305 | |
| 306 | /* Set the color */ |
| 307 | gdk_cairo_set_source_rgba (cr, &fg_color); |
| 308 | |
| 309 | /* Paint the grid label */ |
| 310 | pango_cairo_show_layout (cr, layout); |
| 311 | g_free (caption); |
| 312 | |
| 313 | /* Set the grid line alpha */ |
| 314 | if (i == 0 || i == (num_sections - 1)) |
| 315 | grid_color.alpha = BORDER_ALPHA; |
| 316 | else |
| 317 | grid_color.alpha = GRID_ALPHA; |
| 318 | |
| 319 | /* Draw the line */ |
| 320 | /* Set the color */ |
| 321 | gdk_cairo_set_source_rgba (cr, &grid_color); |
| 322 | |
| 323 | /* Set the grid line path */ |
| 324 | cairo_move_to (cr, x + indent, 0); |
| 325 | cairo_line_to (cr, x + indent, graph->real_draw_height + 4); |
| 326 | } |
| 327 | |
| 328 | /* Paint */ |
| 329 | cairo_stroke (cr); |
| 330 | |
| 331 | g_object_unref (layout); |
| 332 | cairo_destroy (cr); |
| 333 | |
| 334 | return surface; |
| 335 | } |
| 336 | |
| 337 | static void |
| 338 | load_graph_draw (GtkDrawingArea* area, |
| 339 | cairo_t *cr, |
| 340 | int width, |
| 341 | int height, |
| 342 | gpointer data_ptr) |
| 343 | { |
| 344 | LoadGraph * const graph = static_cast<LoadGraph*>(data_ptr); |
| 345 | cairo_surface_t * background; |
| 346 | guint frames_per_unit; |
| 347 | guint render_counter = gsm_graph_get_render_counter (GSM_GRAPH (area)); |
| 348 | double rmargin = gsm_graph_get_right_margin (GSM_GRAPH (area)); |
| 349 | guint num_points = gsm_graph_get_num_points (GSM_GRAPH (area)); |
| 350 | guint indent = gsm_graph_get_indent (GSM_GRAPH (area)); |
| 351 | graph->num_bars = gsm_graph_get_num_bars (GSM_GRAPH (area), height); |
| 352 | |
| 353 | /* Initialize graph dimensions */ |
| 354 | width -= 2 * FRAME_WIDTH; |
| 355 | height -= 2 * FRAME_WIDTH; |
| 356 | |
| 357 | frames_per_unit = width / (num_points - 2); |
| 358 | if (frames_per_unit > 10) frames_per_unit = 10; |
| 359 | gsm_graph_set_frames_per_unit(GSM_GRAPH (area), frames_per_unit); |
| 360 | |
| 361 | graph->graph_dely = (height - 15) / graph->num_bars; /* round to int to avoid AA blur */ |
| 362 | graph->real_draw_height = graph->graph_dely * graph->num_bars; |
| 363 | |
| 364 | /* Number of pixels wide for one sample point */ |
| 365 | const double x_step = double(width - rmargin - graph->indent) / (graph->num_points - 2); |
| 366 | |
| 367 | /* Lines start at the right edge of the drawing, |
| 368 | * a bit outside the clip rectangle. */ |
| 369 | /* Adjustment for smooth movement between samples */ |
| 370 | double x_offset = width - rmargin + FRAME_WIDTH; |
| 371 | |
| 372 | /* Shift the x position of the most recent (shown rightmost) value outside of the clip area in order |
| 373 | to be able to simulate continuous, smooth movement without the line being cut off at its ends */ |
| 374 | x_offset += x_step * (1 - render_counter / double(frames_per_unit)); |
| 375 | |
| 376 | /* Draw background */ |
| 377 | if (!gsm_graph_is_background_set (GSM_GRAPH (graph->disp))) { |
| 378 | background = create_background (graph, width, height); |
| 379 | gsm_graph_set_background (GSM_GRAPH (graph->disp), background); |
| 380 | } else { |
| 381 | background = gsm_graph_get_background (GSM_GRAPH (graph->disp)); |
| 382 | } |
| 383 | |
| 384 | cairo_set_source_surface (cr, background, 0, 0); |
| 385 | cairo_paint (cr); |
| 386 | |
| 387 | /* Set the drawing style */ |
| 388 | cairo_set_line_width (cr, 1); |
| 389 | cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND); |
| 390 | cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND); |
| 391 | |
| 392 | /* Clip the drawing area to the inside of the drawn background */ |
| 393 | cairo_rectangle (cr, |
| 394 | indent + FRAME_WIDTH, |
| 395 | FRAME_WIDTH, |
| 396 | width - rmargin - indent, |
| 397 | graph->real_draw_height); |
| 398 | cairo_clip (cr); |
| 399 | |
| 400 | bool drawStacked = graph->type == LOAD_GRAPH_CPU && GsmApplication::get ().config.draw_stacked; |
| 401 | bool drawSmooth = GsmApplication::get ().config.draw_smooth; |
| 402 | gsm_graph_set_smooth_chart (GSM_GRAPH (area), drawSmooth); |
| 403 | gsm_graph_set_stacked_chart (GSM_GRAPH (area), drawStacked); |
| 404 | |
| 405 | |
| 406 | const double y_base = FRAME_WIDTH; |
| 407 | |
| 408 | /* Draw every graph */ |
| 409 | for (gint j = graph->n - 1; j >= 0; j--) |
| 410 | { |
| 411 | /* Set the color of the currently drawn graph */ |
| 412 | gdk_cairo_set_source_rgba (cr, &(graph->colors [j])); |
| 413 | |
| 414 | /* Start drawing on the right at the correct height */ |
| 415 | cairo_move_to (cr, x_offset, y_base + (1.0f - graph->data[0][j]) * graph->real_draw_height); |
| 416 | |
| 417 | /* Draw the path of the line |
| 418 | Loop starts at 1 because the curve accesses the 0th data point */ |
| 419 | for (guint i = 1; i < num_points; i++) |
| 420 | { |
| 421 | if (graph->data[i][j] == -1.0f) |
| 422 | continue; |
| 423 | |
| 424 | if (drawSmooth) |
| 425 | cairo_curve_to (cr, |
| 426 | x_offset - ((i - 0.5f) * x_step), |
| 427 | y_base + (1.0 - graph->data[i - 1][j]) * graph->real_draw_height, |
| 428 | x_offset - ((i - 0.5f) * x_step), |
| 429 | y_base + (1.0 - graph->data[i][j]) * graph->real_draw_height, |
| 430 | x_offset - (i * x_step), |
| 431 | y_base + (1.0 - graph->data[i][j]) * graph->real_draw_height); |
| 432 | else |
| 433 | cairo_line_to (cr, |
| 434 | x_offset - (i * x_step), |
| 435 | y_base + (1.0 - graph->data[i][j]) * graph->real_draw_height); |
| 436 | } |
| 437 | |
| 438 | if (drawStacked) |
| 439 | { |
| 440 | /* Draw the remaining outline of the area */ |
| 441 | /* Left bottom corner */ |
| 442 | cairo_rel_line_to (cr, 0, y_base + graph->real_draw_height); |
| 443 | /* Right bottom corner. |
| 444 | It's drawn far outside the visible area to avoid a weird bug |
| 445 | where it's not filling the area it should completely */ |
| 446 | cairo_rel_line_to (cr, x_offset * 2, 0); |
| 447 | |
| 448 | cairo_close_path (cr); |
| 449 | cairo_fill (cr); |
| 450 | } |
| 451 | else |
| 452 | { |
| 453 | cairo_stroke (cr); |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | void |
| 459 | load_graph_reset (LoadGraph *graph) |
| 460 | { |
| 461 | graph->iteration = 0; |
| 462 | std::fill (graph->data_block.begin (), graph->data_block.end (), -1.0); |
| 463 | } |
| 464 | |
| 465 | static void |
| 466 | get_load (LoadGraph *graph) |
| 467 | { |
| 468 | guint i; |
| 469 | glibtop_cpu cpu; |
| 470 | |
| 471 | glibtop_get_cpu (&cpu); |
| 472 | |
| 473 | auto NOW = [&]() -> guint64 (&)[GLIBTOP_NCPU][N_CPU_STATES] { |
| 474 | return graph->cpu.times[graph->cpu.now]; |
| 475 | }; |
| 476 | auto LAST = [&]() -> guint64 (&)[GLIBTOP_NCPU][N_CPU_STATES] { |
| 477 | return graph->cpu.times[graph->cpu.now ^ 1]; |
| 478 | }; |
| 479 | |
| 480 | if (graph->n == 1) |
| 481 | { |
| 482 | NOW ()[0][CPU_TOTAL] = cpu.total; |
| 483 | NOW ()[0][CPU_USED] = cpu.user + cpu.nice + cpu.sys; |
| 484 | } |
| 485 | else |
| 486 | { |
| 487 | for (i = 0; i < graph->n; i++) |
| 488 | { |
| 489 | NOW ()[i][CPU_TOTAL] = cpu.xcpu_total[i]; |
| 490 | NOW ()[i][CPU_USED] = cpu.xcpu_user[i] + cpu.xcpu_nice[i] |
| 491 | + cpu.xcpu_sys[i]; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | // on the first call, LAST is 0 |
| 496 | // which means data is set to the average load since boot |
| 497 | // that value has no meaning, we just want all the |
| 498 | // graphs to be aligned, so the CPU graph needs to start |
| 499 | // immediately |
| 500 | bool drawStacked = graph->type == LOAD_GRAPH_CPU && GsmApplication::get ().config.draw_stacked; |
| 501 | |
| 502 | if(graph->iteration != 1) { |
| 503 | for (i = 0; i < graph->n; i++) |
| 504 | { |
| 505 | float load; |
| 506 | float total, used; |
| 507 | gchar *text; |
| 508 | |
| 509 | total = NOW ()[i][CPU_TOTAL] - LAST ()[i][CPU_TOTAL]; |
| 510 | used = NOW ()[i][CPU_USED] - LAST ()[i][CPU_USED]; |
| 511 | |
| 512 | load = used / MAX (total, 1.0f); |
| 513 | |
| 514 | graph->data[0][i] = load; |
| 515 | if (drawStacked) |
| 516 | { |
| 517 | graph->data[0][i] /= graph->n; |
| 518 | if (i > 0) |
| 519 | graph->data[0][i] += graph->data[0][i - 1]; |
| 520 | } |
| 521 | |
| 522 | /* Update label */ |
| 523 | // Translators: CPU usage percentage label: 95.7% |
| 524 | text = g_strdup_printf (_("%.1f%%"), load * 100.0f); |
| 525 | gtk_label_set_text (GTK_LABEL (graph->labels.cpu[i]), text); |
| 526 | g_free (text); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | graph->cpu.now ^= 1; |
| 531 | } |
| 532 | |
| 533 | static void |
| 534 | set_memory_label_and_picker (GtkLabel *label, |
| 535 | GsmColorButton *picker, |
| 536 | guint64 used, |
| 537 | guint64 cached, |
| 538 | guint64 total, |
| 539 | double percent) |
| 540 | { |
| 541 | char *used_text; |
| 542 | char *cached_text; |
| 543 | char *cached_label; |
| 544 | char *total_text; |
| 545 | char *text; |
| 546 | char *tmp_text; |
| 547 | |
| 548 | used_text = format_byte_size (used, GsmApplication::get ().config.resources_memory_in_iec); |
| 549 | cached_text = format_byte_size (cached, GsmApplication::get ().config.resources_memory_in_iec); |
| 550 | total_text = format_byte_size (total, GsmApplication::get ().config.resources_memory_in_iec); |
| 551 | if (total == 0) |
| 552 | { |
| 553 | text = g_strdup (_("not available")); |
| 554 | } |
| 555 | else |
| 556 | { |
| 557 | // xgettext: "540MiB (53 %) of 1.0 GiB" or "540MB (53 %) of 1.0 GB" |
| 558 | text = g_strdup_printf (_("%s (%.1f%%) of %s"), used_text, 100.0 * percent, total_text); |
| 559 | |
| 560 | if (cached != 0) |
| 561 | { |
| 562 | // xgettext: Used cache string, e.g.: "Cache 2.4GiB" or "Cache 2.4GB" |
| 563 | cached_label = g_strdup_printf (_("Cache %s"), cached_text); |
| 564 | tmp_text = g_strdup_printf ("%s\n%s", text, cached_label); |
| 565 | g_free (cached_label); |
| 566 | g_free (text); |
| 567 | text = tmp_text; |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | gtk_label_set_text (label, text); |
| 572 | g_free (used_text); |
| 573 | g_free (cached_text); |
| 574 | g_free (total_text); |
| 575 | g_free (text); |
| 576 | |
| 577 | if (picker) |
| 578 | gsm_color_button_set_fraction (picker, percent); |
| 579 | } |
| 580 | |
| 581 | static void |
| 582 | get_memory (LoadGraph *graph) |
| 583 | { |
| 584 | float mempercent, swappercent; |
| 585 | |
| 586 | glibtop_mem mem; |
| 587 | glibtop_swap swap; |
| 588 | |
| 589 | glibtop_get_mem (&mem); |
| 590 | glibtop_get_swap (&swap); |
| 591 | |
| 592 | /* There's no swap on LiveCD : 0.0f is better than NaN :) */ |
| 593 | swappercent = (swap.total ? (float)swap.used / (float)swap.total : 0.0f); |
| 594 | mempercent = (float)mem.user / (float)mem.total; |
| 595 | set_memory_label_and_picker (GTK_LABEL (graph->labels.memory), |
| 596 | GSM_COLOR_BUTTON (graph->mem_color_picker), |
| 597 | mem.user, mem.cached, mem.total, mempercent); |
| 598 | |
| 599 | set_memory_label_and_picker (GTK_LABEL (graph->labels.swap), |
| 600 | GSM_COLOR_BUTTON (graph->swap_color_picker), |
| 601 | swap.used, 0, swap.total, swappercent); |
| 602 | |
| 603 | gtk_widget_set_sensitive (GTK_WIDGET (graph->swap_color_picker), swap.total > 0); |
| 604 | |
| 605 | if(graph->iteration != 1) { |
| 606 | graph->data[0][0] = graph->translate_to_log_partial_if_needed (mempercent); |
| 607 | graph->data[0][1] = swap.total > 0 ? graph->translate_to_log_partial_if_needed (swappercent) : -1.0; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | /* Nice Numbers for Graph Labels after Paul Heckbert |
| 612 | nicenum: find a "nice" number approximately equal to x. |
| 613 | Round the number if round=1, take ceiling if round=0 */ |
| 614 | |
| 615 | static double |
| 616 | nicenum (double x, |
| 617 | int round) |
| 618 | { |
| 619 | int expv; /* exponent of x */ |
| 620 | double f; /* fractional part of x */ |
| 621 | double nf; /* nice, rounded fraction */ |
| 622 | |
| 623 | expv = floor (log10 (x)); |
| 624 | f = x / pow (10.0, expv); /* between 1 and 10 */ |
| 625 | if (round) |
| 626 | { |
| 627 | if (f < 1.5) |
| 628 | nf = 1.0; |
| 629 | else if (f < 3.0) |
| 630 | nf = 2.0; |
| 631 | else if (f < 7.0) |
| 632 | nf = 5.0; |
| 633 | else |
| 634 | nf = 10.0; |
| 635 | } |
| 636 | else |
| 637 | { |
| 638 | if (f <= 1.0) |
| 639 | nf = 1.0; |
| 640 | else if (f <= 2.0) |
| 641 | nf = 2.0; |
| 642 | else if (f <= 5.0) |
| 643 | nf = 5.0; |
| 644 | else |
| 645 | nf = 10.0; |
| 646 | } |
| 647 | return nf * pow (10.0, expv); |
| 648 | } |
| 649 | |
| 650 | static void |
| 651 | dynamic_scale (LoadGraph *graph, |
| 652 | std::vector<unsigned> *values, |
| 653 | guint64 *max, |
| 654 | guint64 din, |
| 655 | guint64 dout, |
| 656 | gboolean in_bits) |
| 657 | { |
| 658 | graph->data[0][0] = 1.0f * din / *max; |
| 659 | graph->data[0][1] = 1.0f * dout / *max; |
| 660 | |
| 661 | guint64 dmax = std::max (din, dout); |
| 662 | |
| 663 | if (graph->latest == 0) |
| 664 | values->at (graph->num_points - 1) = dmax; |
| 665 | else |
| 666 | values->at (graph->latest - 1) = dmax; |
| 667 | |
| 668 | guint64 new_max; |
| 669 | // both way, new_max is the greatest value |
| 670 | if (dmax >= *max) |
| 671 | new_max = dmax; |
| 672 | else |
| 673 | new_max = *std::max_element (&values->at (0), |
| 674 | &values->at (graph->num_points - 1)); |
| 675 | |
| 676 | // |
| 677 | // Round maximum |
| 678 | // |
| 679 | |
| 680 | const guint64 bak_max (new_max); |
| 681 | |
| 682 | if (in_bits) |
| 683 | { |
| 684 | // nice number is for the ticks |
| 685 | unsigned ticks = graph->num_bars; |
| 686 | |
| 687 | if (graph->num_bars == 0) |
| 688 | return; |
| 689 | |
| 690 | // gets messy at low values due to division by 8 |
| 691 | guint64 bit_max = std::max (new_max * 8, G_GUINT64_CONSTANT (10000)); |
| 692 | |
| 693 | // our tick size leads to max |
| 694 | double d = nicenum (bit_max / ticks, 0); |
| 695 | bit_max = ticks * d; |
| 696 | new_max = bit_max / 8; |
| 697 | |
| 698 | procman_debug ("bak*8 %" G_GUINT64_FORMAT ", ticks %d, d %f" |
| 699 | ", bit_max %" G_GUINT64_FORMAT ", new_max %" G_GUINT64_FORMAT, |
| 700 | bak_max * 8, ticks, d, bit_max, new_max); |
| 701 | } |
| 702 | else |
| 703 | { |
| 704 | // round up to get some extra space |
| 705 | // yes, it can overflow |
| 706 | new_max = 1.1 * new_max; |
| 707 | // make sure max is not 0 to avoid / 0 |
| 708 | // default to 1 KiB |
| 709 | new_max = std::max (new_max, G_GUINT64_CONSTANT (1024)); |
| 710 | |
| 711 | // decompose new_max = coef10 * 2**(base10 * 10) |
| 712 | // where coef10 and base10 are integers and coef10 < 2**10 |
| 713 | // |
| 714 | // e.g: ceil(100.5 KiB) = 101 KiB = 101 * 2**(1 * 10) |
| 715 | // where base10 = 1, coef10 = 101, pow2 = 16 |
| 716 | |
| 717 | guint64 pow2 = std::floor (log2 (new_max)); |
| 718 | guint64 base10 = pow2 / 10.0; |
| 719 | guint64 coef10 = std::ceil (new_max / double (G_GUINT64_CONSTANT (1) << (base10 * 10))); |
| 720 | g_assert (new_max <= (coef10 * (G_GUINT64_CONSTANT (1) << (base10 * 10)))); |
| 721 | |
| 722 | // then decompose coef10 = x * 10**factor10 |
| 723 | // where factor10 is integer and x < 10 |
| 724 | // so we new_max has only 1 significant digit |
| 725 | |
| 726 | guint64 factor10 = std::pow (10.0, std::floor (std::log10 (coef10))); |
| 727 | coef10 = std::ceil (coef10 / double (factor10)) * factor10; |
| 728 | |
| 729 | new_max = coef10 * (G_GUINT64_CONSTANT (1) << guint64 (base10 * 10)); |
| 730 | procman_debug ("bak %" G_GUINT64_FORMAT " new_max %" G_GUINT64_FORMAT |
| 731 | "pow2 %" G_GUINT64_FORMAT " coef10 %" G_GUINT64_FORMAT, |
| 732 | bak_max, new_max, pow2, coef10); |
| 733 | } |
| 734 | |
| 735 | // if max is the same or has decreased but not so much, don't |
| 736 | // do anything to avoid rescaling |
| 737 | if ((0.8 * graph->net.max) < new_max && new_max <= graph->net.max) |
| 738 | return; |
| 739 | |
| 740 | const double scale = 1.0f * *max / new_max; |
| 741 | |
| 742 | for (size_t i = 0; i < graph->num_points; i++) |
| 743 | if (graph->data[i][0] >= 0.0f) |
| 744 | { |
| 745 | graph->data[i][0] *= scale; |
| 746 | graph->data[i][1] *= scale; |
| 747 | } |
| 748 | |
| 749 | procman_debug ("rescale dmax = %" G_GUINT64_FORMAT |
| 750 | " max = %" G_GUINT64_FORMAT |
| 751 | " new_max = %" G_GUINT64_FORMAT, |
| 752 | dmax, *max, new_max); |
| 753 | |
| 754 | *max = new_max; |
| 755 | |
| 756 | // force the graph background to be redrawn now that scale has changed |
| 757 | graph->clear_background (); |
| 758 | } |
| 759 | |
| 760 | static guint64 |
| 761 | get_hash64 (const gchar*c_str) |
| 762 | { |
| 763 | // Fowler–Noll–Vo FNV-1 64-bit hash: |
| 764 | |
| 765 | guint64 hash = 0xcbf29ce484222325L; |
| 766 | |
| 767 | while (gchar c = *c_str++) |
| 768 | { |
| 769 | hash = (hash * 0x00000100000001B3L) ^ c; |
| 770 | } |
| 771 | |
| 772 | return hash; |
| 773 | } |
| 774 | |
| 775 | static void |
| 776 | handle_dynamic_max_value (LoadGraph *graph, |
| 777 | std::vector<unsigned> *values, |
| 778 | guint64 *max, |
| 779 | guint64 *last_in, |
| 780 | guint64 *last_out, |
| 781 | guint64 in, |
| 782 | guint64 out, |
| 783 | guint64 *graph_time, |
| 784 | guint64 hash, |
| 785 | guint64 *graph_hash, |
| 786 | gboolean in_bits, |
| 787 | gboolean totals_in_bits, |
| 788 | GtkLabel *label_in, |
| 789 | GtkLabel *label_out, |
| 790 | GtkLabel *label_in_total, |
| 791 | GtkLabel *label_out_total) |
| 792 | { |
| 793 | guint64 time = g_get_monotonic_time (); |
| 794 | guint64 din, dout; |
| 795 | |
| 796 | if (in >= *last_in && out >= *last_out && |
| 797 | (graph_hash == NULL || hash == *graph_hash) && |
| 798 | *graph_time != 0) |
| 799 | { |
| 800 | float dtime = ((double) (time - *graph_time)) / G_USEC_PER_SEC; |
| 801 | din = static_cast<guint64>((in - *last_in) / dtime); |
| 802 | dout = static_cast<guint64>((out - *last_out) / dtime); |
| 803 | } |
| 804 | else |
| 805 | { |
| 806 | /* Don't calc anything if new data is less than old (interface |
| 807 | removed, counters reset, ...) or if it is the first time */ |
| 808 | din = 0; |
| 809 | dout = 0; |
| 810 | } |
| 811 | |
| 812 | *last_in = in; |
| 813 | *last_out = out; |
| 814 | *graph_time = time; |
| 815 | if (graph_hash != NULL) |
| 816 | *graph_hash = hash; |
| 817 | |
| 818 | dynamic_scale (graph, values, max, din, dout, in_bits); |
| 819 | |
| 820 | gtk_label_set_text (GTK_LABEL (label_in), procman::format_rate (din, in_bits).c_str ()); |
| 821 | gtk_label_set_text (GTK_LABEL (label_in_total), procman::format_volume (in, totals_in_bits).c_str ()); |
| 822 | |
| 823 | gtk_label_set_text (GTK_LABEL (label_out), procman::format_rate (dout, in_bits).c_str ()); |
| 824 | gtk_label_set_text (GTK_LABEL (label_out_total), procman::format_volume (out, totals_in_bits).c_str ()); |
| 825 | } |
| 826 | |
| 827 | static void |
| 828 | get_net (LoadGraph *graph) |
| 829 | { |
| 830 | glibtop_netlist netlist; |
| 831 | guint32 i; |
| 832 | guint64 in = 0, out = 0; |
| 833 | guint64 hash = 1; |
| 834 | char **ifnames; |
| 835 | |
| 836 | ifnames = glibtop_get_netlist (&netlist); |
| 837 | |
| 838 | for (i = 0; i < netlist.number; ++i) |
| 839 | { |
| 840 | glibtop_netload netload; |
| 841 | glibtop_get_netload (&netload, ifnames[i]); |
| 842 | |
| 843 | if (netload.if_flags & (1 << GLIBTOP_IF_FLAGS_LOOPBACK)) |
| 844 | continue; |
| 845 | |
| 846 | /* Skip interfaces without any IPv4/IPv6 address (or |
| 847 | those with only a LINK ipv6 addr) However we need to |
| 848 | be able to exclude these while still keeping the |
| 849 | value so when they get online (with NetworkManager |
| 850 | for example) we don't get a sudden peak. Once we're |
| 851 | able to get this, ignoring down interfaces will be |
| 852 | possible too. */ |
| 853 | if (not ((netload.flags & (1 << GLIBTOP_NETLOAD_ADDRESS6)) |
| 854 | and netload.scope6 != GLIBTOP_IF_IN6_SCOPE_LINK) |
| 855 | and not (netload.flags & (1 << GLIBTOP_NETLOAD_ADDRESS))) |
| 856 | continue; |
| 857 | |
| 858 | /* Don't skip interfaces that are down (GLIBTOP_IF_FLAGS_UP) |
| 859 | to avoid spikes when they are brought up */ |
| 860 | |
| 861 | in += netload.bytes_in; |
| 862 | out += netload.bytes_out; |
| 863 | hash += get_hash64 (ifnames[i]); |
| 864 | } |
| 865 | |
| 866 | g_strfreev (ifnames); |
| 867 | |
| 868 | if(graph->iteration != 1) { |
| 869 | handle_dynamic_max_value (graph, &graph->net.values, &graph->net.max, &graph->net.last_in, |
| 870 | &graph->net.last_out, in, out, &graph->net.time, |
| 871 | hash, &graph->net.last_hash, |
| 872 | GsmApplication::get ().config.network_in_bits, |
| 873 | GsmApplication::get ().config.network_total_in_bits, |
| 874 | graph->labels.net_in, graph->labels.net_out, |
| 875 | graph->labels.net_in_total, graph->labels.net_out_total); |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | static void |
| 880 | get_disk (LoadGraph *graph) |
| 881 | { |
| 882 | glibtop_disk disk; |
| 883 | gint32 i; |
| 884 | guint64 read = 0, write = 0; |
| 885 | |
| 886 | glibtop_get_disk (&disk); |
| 887 | |
| 888 | for (i = 0; i < glibtop_global_server->ndisk; i++) |
| 889 | { |
| 890 | read += disk.xdisk_sectors_read[i]; |
| 891 | write += disk.xdisk_sectors_write[i]; |
| 892 | } |
| 893 | |
| 894 | // These sectors are standard unix 512 byte sectors, not the actual disk sectors. |
| 895 | read *= 512; |
| 896 | write *= 512; |
| 897 | |
| 898 | if(graph->iteration != 1) { |
| 899 | handle_dynamic_max_value (graph, &graph->disk.values, &graph->disk.max, &graph->disk.last_read, |
| 900 | &graph->disk.last_write, read, write, &graph->disk.time, 0, NULL, |
| 901 | FALSE, FALSE, graph->labels.disk_read, graph->labels.disk_write, |
| 902 | graph->labels.disk_read_total, graph->labels.disk_write_total); |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | int |
| 907 | load_graph_update_data (LoadGraph *graph) |
| 908 | { |
| 909 | // Rotate data one element down. |
| 910 | std::rotate (graph->data.begin (), |
| 911 | graph->data.end () - 1, |
| 912 | graph->data.end ()); |
| 913 | |
| 914 | // Update rotation counter. |
| 915 | graph->latest = (graph->latest + 1) % graph->num_points; |
| 916 | graph->iteration++; |
| 917 | |
| 918 | // Replace the 0th element |
| 919 | switch (graph->type) |
| 920 | { |
| 921 | case LOAD_GRAPH_CPU: |
| 922 | get_load (graph); |
| 923 | break; |
| 924 | |
| 925 | case LOAD_GRAPH_MEM: |
| 926 | get_memory (graph); |
| 927 | break; |
| 928 | |
| 929 | case LOAD_GRAPH_NET: |
| 930 | get_net (graph); |
| 931 | break; |
| 932 | |
| 933 | case LOAD_GRAPH_DISK: |
| 934 | get_disk (graph); |
| 935 | break; |
| 936 | |
| 937 | default: |
| 938 | g_assert_not_reached (); |
| 939 | } |
| 940 | return 0; |
| 941 | } |
| 942 | |
| 943 | static void |
| 944 | load_graph_destroy (GtkWidget*, |
| 945 | gpointer data_ptr) |
| 946 | { |
| 947 | LoadGraph * const graph = static_cast<LoadGraph*>(data_ptr); |
| 948 | |
| 949 | delete graph; |
| 950 | } |
| 951 | |
| 952 | LoadGraph::LoadGraph(guint type) |
| 953 | : |
| 954 | indent (18.0), |
| 955 | n (0), |
| 956 | type (type), |
| 957 | speed (GsmApplication::get ().config.graph_update_interval), |
| 958 | num_points (GsmApplication::get ().config.graph_data_points + 2), |
| 959 | latest (0), |
| 960 | graph_dely (0), |
| 961 | num_bars (0), |
| 962 | real_draw_height (0), |
| 963 | colors (), |
| 964 | data_block (), |
| 965 | data (), |
| 966 | main_widget (NULL), |
| 967 | disp (NULL), |
| 968 | labels (), |
| 969 | mem_color_picker (NULL), |
| 970 | swap_color_picker (NULL), |
| 971 | font_settings (Gio::Settings::create (FONT_SETTINGS_SCHEMA)), |
| 972 | cpu (), |
| 973 | net (), |
| 974 | disk () |
| 975 | { |
| 976 | font_settings->signal_changed (FONT_SETTING_SCALING).connect ([this](const Glib::ustring&) { |
| 977 | load_graph_rescale (this); |
| 978 | }); |
| 979 | |
| 980 | switch (type) |
| 981 | { |
| 982 | case LOAD_GRAPH_CPU: |
| 983 | cpu = CPU {}; |
| 984 | n = GsmApplication::get ().config.num_cpus; |
| 985 | |
| 986 | for (guint i = 0; i < G_N_ELEMENTS (labels.cpu); ++i) |
| 987 | labels.cpu[i] = make_tnum_label (); |
| 988 | |
| 989 | break; |
| 990 | |
| 991 | case LOAD_GRAPH_MEM: |
| 992 | n = 2; |
| 993 | labels.memory = init_tnum_label (10, GTK_ALIGN_START); |
| 994 | labels.swap = init_tnum_label (10, GTK_ALIGN_START); |
| 995 | break; |
| 996 | |
| 997 | case LOAD_GRAPH_NET: |
| 998 | net = NET {}; |
| 999 | n = 2; |
| 1000 | net.max = 1; |
| 1001 | labels.net_in = init_tnum_label (10, GTK_ALIGN_END); |
| 1002 | labels.net_in_total = init_tnum_label (10, GTK_ALIGN_END); |
| 1003 | labels.net_out = init_tnum_label (10, GTK_ALIGN_END); |
| 1004 | labels.net_out_total = init_tnum_label (10, GTK_ALIGN_END); |
| 1005 | break; |
| 1006 | |
| 1007 | case LOAD_GRAPH_DISK: |
| 1008 | disk = DISK {}; |
| 1009 | n = 2; |
| 1010 | disk.max = 1; |
| 1011 | labels.disk_read = init_tnum_label (16, GTK_ALIGN_END); |
| 1012 | labels.disk_read_total = init_tnum_label (16, GTK_ALIGN_END); |
| 1013 | labels.disk_write = init_tnum_label (10, GTK_ALIGN_END); |
| 1014 | labels.disk_write_total = init_tnum_label (10, GTK_ALIGN_END); |
| 1015 | break; |
| 1016 | } |
| 1017 | |
| 1018 | colors.resize (n); |
| 1019 | |
| 1020 | disp = GSM_GRAPH (gsm_graph_new ()); |
| 1021 | gsm_graph_set_speed (disp, speed); |
| 1022 | gsm_graph_set_data_function (disp, (GSourceFunc)load_graph_update_data, this); |
| 1023 | gsm_graph_set_num_points (disp, num_points); |
| 1024 | |
| 1025 | switch (type) |
| 1026 | { |
| 1027 | case LOAD_GRAPH_CPU: |
| 1028 | memcpy (&colors[0], GsmApplication::get ().config.cpu_color, |
| 1029 | n * sizeof colors[0]); |
| 1030 | gsm_graph_set_max_value (disp, 100); |
| 1031 | break; |
| 1032 | |
| 1033 | case LOAD_GRAPH_MEM: |
| 1034 | colors[0] = GsmApplication::get ().config.mem_color; |
| 1035 | colors[1] = GsmApplication::get ().config.swap_color; |
| 1036 | mem_color_picker = gsm_color_button_new (&colors[0], |
| 1037 | GSMCP_TYPE_PIE); |
| 1038 | swap_color_picker = gsm_color_button_new (&colors[1], |
| 1039 | GSMCP_TYPE_PIE); |
| 1040 | gsm_graph_set_max_value (disp, 100); |
| 1041 | break; |
| 1042 | |
| 1043 | case LOAD_GRAPH_NET: |
| 1044 | net.values = std::vector<unsigned>(num_points); |
| 1045 | colors[0] = GsmApplication::get ().config.net_in_color; |
| 1046 | colors[1] = GsmApplication::get ().config.net_out_color; |
| 1047 | gsm_graph_set_max_value (disp, this->net.max); |
| 1048 | break; |
| 1049 | |
| 1050 | case LOAD_GRAPH_DISK: |
| 1051 | disk.values = std::vector<unsigned>(num_points); |
| 1052 | colors[0] = GsmApplication::get ().config.disk_read_color; |
| 1053 | colors[1] = GsmApplication::get ().config.disk_write_color; |
| 1054 | gsm_graph_set_max_value (disp, this->disk.max); |
| 1055 | break; |
| 1056 | } |
| 1057 | |
| 1058 | main_widget = GTK_BOX (gtk_box_new (GTK_ORIENTATION_VERTICAL, 6)); |
| 1059 | gtk_widget_set_size_request (GTK_WIDGET (main_widget), -1, GRAPH_MIN_HEIGHT); |
| 1060 | |
| 1061 | gtk_widget_set_vexpand (GTK_WIDGET (disp), TRUE); |
| 1062 | gtk_widget_set_hexpand (GTK_WIDGET (disp), TRUE); |
| 1063 | gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (disp), |
| 1064 | load_graph_draw, |
| 1065 | this, |
| 1066 | NULL); |
| 1067 | g_signal_connect (G_OBJECT (disp), "destroy", |
| 1068 | G_CALLBACK (load_graph_destroy), this); |
| 1069 | gtk_box_prepend (main_widget, GTK_WIDGET (disp)); |
| 1070 | |
| 1071 | data = std::vector<double*>(num_points); |
| 1072 | /* Allocate data in a contiguous block */ |
| 1073 | data_block = std::vector<double>(n * num_points, -1.0); |
| 1074 | |
| 1075 | for (guint i = 0; i < num_points; ++i) |
| 1076 | data[i] = &data_block[0] + i * n; |
| 1077 | |
| 1078 | } |
| 1079 | |
| 1080 | LoadGraph::~LoadGraph() |
| 1081 | { |
| 1082 | load_graph_stop (this); |
| 1083 | } |
| 1084 | |
| 1085 | void |
| 1086 | load_graph_start (LoadGraph *graph) |
| 1087 | { |
| 1088 | graph->iteration = 0; |
| 1089 | gsm_graph_start (GSM_GRAPH (graph->disp)); |
| 1090 | } |
| 1091 | |
| 1092 | void |
| 1093 | load_graph_stop (LoadGraph *graph) |
| 1094 | { |
| 1095 | /* don't draw anymore, but continue to poll */ |
| 1096 | gsm_graph_stop (GSM_GRAPH (graph->disp)); |
| 1097 | } |
| 1098 | |
| 1099 | void |
| 1100 | load_graph_change_speed (LoadGraph *graph, |
| 1101 | guint new_speed) |
| 1102 | { |
| 1103 | gsm_graph_set_speed (GSM_GRAPH (graph->disp), new_speed); |
| 1104 | // LoadGraph->speed and GsmGraph->speed are different |
| 1105 | graph->speed = new_speed; |
| 1106 | } |
| 1107 | |
| 1108 | void |
| 1109 | load_graph_change_num_points (LoadGraph *graph, |
| 1110 | guint new_num_points) |
| 1111 | { |
| 1112 | // Don't do anything if the value didn't change. |
| 1113 | if (graph->num_points == new_num_points) |
| 1114 | return; |
| 1115 | |
| 1116 | // Sort the values in the data_block vector in the order they were accessed in by the pointers in data. |
| 1117 | std::rotate (graph->data_block.begin (), |
| 1118 | graph->data_block.begin () + (graph->num_points - graph->latest) * graph->n, |
| 1119 | graph->data_block.end ()); |
| 1120 | |
| 1121 | // Reset rotation counter. |
| 1122 | graph->latest = 0; |
| 1123 | |
| 1124 | // Resize the vectors to the new amount of data points. |
| 1125 | // Fill the new values with -1. |
| 1126 | graph->data.resize (new_num_points); |
| 1127 | graph->data_block.resize (graph->n * new_num_points, -1.0); |
| 1128 | if (graph->type == LOAD_GRAPH_NET) |
| 1129 | graph->net.values.resize (new_num_points); |
| 1130 | else if (graph->type == LOAD_GRAPH_DISK) |
| 1131 | graph->disk.values.resize (new_num_points); |
| 1132 | |
| 1133 | // Replace the pointers in data, to match the new data_block values. |
| 1134 | for (guint i = 0; i < new_num_points; ++i) |
| 1135 | graph->data[i] = &graph->data_block[0] + i * graph->n; |
| 1136 | |
| 1137 | // Set the actual number of data points to be used by the graph. |
| 1138 | graph->num_points = new_num_points; |
| 1139 | gsm_graph_set_num_points (graph->disp, new_num_points); |
| 1140 | |
| 1141 | // Force the scale to be redrawn. |
| 1142 | graph->clear_background (); |
| 1143 | } |
| 1144 | |
| 1145 | LoadGraphLabels* |
| 1146 | load_graph_get_labels (LoadGraph *graph) |
| 1147 | { |
| 1148 | return &graph->labels; |
| 1149 | } |
| 1150 | |
| 1151 | GtkBox* |
| 1152 | load_graph_get_widget (LoadGraph *graph) |
| 1153 | { |
| 1154 | return graph->main_widget; |
| 1155 | } |
| 1156 | |
| 1157 | GsmColorButton* |
| 1158 | load_graph_get_mem_color_picker (LoadGraph *graph) |
| 1159 | { |
| 1160 | return graph->mem_color_picker; |
| 1161 | } |
| 1162 | |
| 1163 | GsmColorButton* |
| 1164 | load_graph_get_swap_color_picker (LoadGraph *graph) |
| 1165 | { |
| 1166 | return graph->swap_color_picker; |
| 1167 | } |