histo-svg.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include "histo-svg.h"
  17. static uint64_t absolute_start_time;
  18. static uint64_t start_time;
  19. static uint64_t absolute_end_time;
  20. static uint64_t end_time;
  21. static char *out_path;
  22. static FILE *out_file;
  23. static void add_region(worker_mode color, uint64_t start, uint64_t end, unsigned worker)
  24. {
  25. float starty, endy, startx, endx;
  26. starty = SVG_BORDERY + (SVG_THICKNESS + SVG_GAP)*worker;
  27. endy = starty + SVG_THICKNESS;
  28. double ratio_start, ratio_end;
  29. ratio_start = (double)(start - start_time) / (double)(end_time - start_time);
  30. ratio_end = (double)(end - start_time) / (double)(end_time - start_time);
  31. startx = (float)(SVG_BORDERX + ratio_start*(SVG_WIDTH - 2*SVG_BORDERX));
  32. endx = (float)(SVG_BORDERX + ratio_end*(SVG_WIDTH - 2*SVG_BORDERX));
  33. // printf("startx %d endx %d ratio %f %f starty %d endy %d\n", startx, endx, ratio_start, ratio_end, starty, endy);
  34. char *color_str;
  35. switch (color) {
  36. case WORKING:
  37. color_str = "green";
  38. break;
  39. case IDLE:
  40. default:
  41. color_str = "red";
  42. break;
  43. }
  44. fprintf(out_file, "<rect x=\"%fcm\" y=\"%fcm\" width=\"%fcm\" height=\"%fcm\" stroke-width=\"10\%%\" stroke=\"black\" fill=\"%s\"/>\n", startx, starty, endx - startx, endy -starty, color_str);
  45. }
  46. static void display_worker(event_list_t events, unsigned worker, char *worker_name)
  47. {
  48. uint64_t prev = start_time;
  49. worker_mode prev_state = IDLE;
  50. fprintf(out_file, "<text x=\"%fcm\" y=\"%fcm\" font-size=\"%fcm\" fill=\"blue\" text-anchor=\"center\" > <tspan font-weight=\"bold\">%s</tspan></text>\n", 0.5f*SVG_BORDERX, SVG_BORDERY + (SVG_THICKNESS + SVG_GAP)*worker + 0.5f*SVG_THICKNESS, SVG_THICKNESS/4.0f, worker_name);
  51. event_itor_t i;
  52. for (i = event_list_begin(events);
  53. i != event_list_end(events);
  54. i = event_list_next(i))
  55. {
  56. add_region(prev_state, prev, i->time, worker);
  57. prev = i->time;
  58. prev_state = i->mode;
  59. }
  60. }
  61. static void display_workq_evolution(workq_list_t taskq, unsigned nworkers, unsigned maxq_size)
  62. {
  63. float endy, starty;
  64. starty = SVG_BORDERY + (SVG_THICKNESS + SVG_GAP)*nworkers;
  65. endy = starty + SVG_THICKNESS;
  66. fprintf(out_file, "<line x1=\"%fcm\" y1=\"%fcm\" x2=\"%fcm\" y2=\"%fcm\" stroke=\"black\" stroke-width=\"100\%%\" />\n", SVG_BORDERX, endy, SVG_WIDTH - SVG_BORDERX, endy);
  67. fprintf(out_file, "<line x1=\"%fcm\" y1=\"%fcm\" x2=\"%fcm\" y2=\"%fcm\" stroke=\"black\" stroke-width=\"100\%%\" />\n", SVG_BORDERX, starty, SVG_BORDERX, endy);
  68. float prevx, prevy;
  69. prevx = SVG_BORDERX;
  70. prevy = endy;
  71. workq_itor_t i;
  72. for (i = workq_list_begin(taskq);
  73. i != workq_list_end(taskq);
  74. i = workq_list_next(i))
  75. {
  76. float event_pos;
  77. double event_ratio;
  78. float y;
  79. event_ratio = ( i->time - start_time )/ (double)(end_time - start_time);
  80. event_pos = (SVG_BORDERX + event_ratio*(SVG_WIDTH - 2*SVG_BORDERX));
  81. double qratio;
  82. qratio = ((double)(i->current_size))/((double)maxq_size);
  83. y = ((double)endy - qratio *((double)SVG_THICKNESS));
  84. fprintf(out_file, "<line x1=\"%fcm\" y1=\"%fcm\" x2=\"%fcm\" y2=\"%fcm\" stroke=\"black\" stroke-width=\"30\%%\" />\n", prevx, prevy, event_pos, y);
  85. prevx = event_pos;
  86. prevy = y;
  87. }
  88. fprintf(out_file, "<line x1=\"%fcm\" y1=\"%fcm\" x2=\"%fcm\" y2=\"%fcm\" stroke=\"black\" stroke-width=\"100\%%\" />\n", prevx, prevy, SVG_BORDERX, endy);
  89. }
  90. void svg_output_file_init(void)
  91. {
  92. /* create a new file */
  93. out_file = fopen(out_path, "w+");
  94. /* create some header for a valid svg file */
  95. fprintf(out_file, "<?xml version=\"1.0\" standalone=\"no\"?>\n");
  96. fprintf(out_file, "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n");
  97. fprintf(out_file, "<svg width=\"%fcm\" height=\"%fcm\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">\n", SVG_WIDTH, SVG_HEIGHT);
  98. /* a little description is not too much */
  99. fprintf(out_file, "<desc>Gantt diagram ...</desc>\n");
  100. }
  101. void svg_output_file_terminate(void)
  102. {
  103. fprintf(out_file, "</svg>\n");
  104. /* close the file */
  105. fclose(out_file);
  106. }
  107. void svg_engine_generate_output(event_list_t *events, workq_list_t taskq, char **worker_name,
  108. unsigned nworkers, unsigned maxq_size,
  109. uint64_t _start_time, uint64_t _end_time, char *path)
  110. {
  111. out_path = path;
  112. unsigned worker;
  113. start_time = _start_time;
  114. absolute_start_time = _start_time;
  115. end_time = _end_time;
  116. absolute_end_time = _end_time;
  117. svg_output_file_init();
  118. for (worker = 0; worker < nworkers; worker++)
  119. {
  120. display_worker(events[worker], worker, worker_name[worker]);
  121. }
  122. display_workq_evolution(taskq, nworkers, maxq_size);
  123. svg_output_file_terminate();
  124. }