perf_count_policy.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU 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. * StarPU 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 "sc_hypervisor_policy.h"
  17. #include <stdio.h>
  18. #include <inttypes.h>
  19. #include <unistd.h>
  20. #include <stdlib.h>
  21. #include <sys/mman.h>
  22. #include <asm/unistd.h>
  23. #include <linux/perf_event.h>
  24. #include <linux/kernel.h>
  25. #include <omp.h>
  26. #include <sys/ioctl.h>
  27. struct perf_event_attr pe_instr[STARPU_NMAXWORKERS];
  28. /* struct perf_event_attr pe_cycles[STARPU_NMAXWORKERS]; */
  29. /* struct perf_event_attr pe_cache_misses[STARPU_NMAXWORKERS]; */
  30. /* struct perf_event_attr pe_cache_refs[STARPU_NMAXWORKERS]; */
  31. /* struct perf_event_attr pe_branch_instr[STARPU_NMAXWORKERS]; */
  32. struct perf_event_attr pe_fps[STARPU_NMAXWORKERS];
  33. int fd_instr[STARPU_NMAXWORKERS];
  34. /* int fd_cycles[STARPU_NMAXWORKERS]; */
  35. /* int fd_cache_misses[STARPU_NMAXWORKERS]; */
  36. /* int fd_cache_refs[STARPU_NMAXWORKERS]; */
  37. /* int fd_branch_instr[STARPU_NMAXWORKERS]; */
  38. int fd_fps[STARPU_NMAXWORKERS];
  39. unsigned perf_event_opened[STARPU_NMAXWORKERS];
  40. long long total_instr[STARPU_NMAX_SCHED_CTXS];
  41. /* long long total_cycles[STARPU_NMAX_SCHED_CTXS]; */
  42. /* long long total_time[STARPU_NMAX_SCHED_CTXS]; */
  43. /* long long total_cache_misses[STARPU_NMAX_SCHED_CTXS]; */
  44. /* long long total_cache_refs[STARPU_NMAX_SCHED_CTXS]; */
  45. /* long long total_branch_instr[STARPU_NMAX_SCHED_CTXS]; */
  46. long long total_fps[STARPU_NMAX_SCHED_CTXS];
  47. struct read_format
  48. {
  49. uint64_t value; /* The value of the event */
  50. uint64_t time_enabled; /* if PERF_FORMAT_TOTAL_TIME_ENABLED */
  51. uint64_t time_running; /* if PERF_FORMAT_TOTAL_TIME_RUNNING */
  52. uint64_t id; /* if PERF_FORMAT_ID */
  53. };
  54. static long perf_event_open(struct perf_event_attr *attr, pid_t pid, int cpu,
  55. int group_fd, unsigned long flags)
  56. {
  57. int ret = syscall(__NR_perf_event_open, attr, pid, cpu, group_fd, flags);
  58. return ret;
  59. }
  60. void print_results_for_worker(int workerid, unsigned sched_ctx, struct starpu_task *task)
  61. {
  62. ssize_t rread;
  63. long long instr, /*cycles, cache_misses, cache_refs, branch_instr,*/ fps;
  64. rread = read(fd_instr[workerid], &instr, sizeof(instr));
  65. assert(rread == sizeof(instr));
  66. /* read(fd_cycles[workerid], &cycles, sizeof(long long)); */
  67. /* read(fd_cache_misses[workerid], &cache_misses, sizeof(long long)); */
  68. /* read(fd_cache_refs[workerid], &cache_refs, sizeof(long long)); */
  69. /* read(fd_branch_instr[workerid], &branch_instr, sizeof(long long)); */
  70. rread = read(fd_fps[workerid], &fps, sizeof(long long));
  71. assert(rread == sizeof(long long));
  72. total_instr[sched_ctx] += instr;
  73. /* total_cycles[sched_ctx] += cycles; */
  74. /* total_cache_misses[sched_ctx] += cache_misses; */
  75. /* total_cache_refs[sched_ctx] += cache_refs; */
  76. /* total_branch_instr[sched_ctx] += branch_instr; */
  77. total_fps[sched_ctx] += fps;
  78. printf("Instrs %lf M instr of worker %lf M\n", (double)total_instr[sched_ctx]/1000000,
  79. (double)instr/1000000);
  80. printf("Fps %lf M curr fps %lf M \n", (double)total_fps[sched_ctx]/1000000,
  81. (double)fps/1000000);
  82. printf("Task Flops %lf k %s \n", task->flops/1000, (task->cl && task->cl->model) ? task->cl->model->symbol : "task null");
  83. printf("-------------------------------------------\n");
  84. }
  85. void print_results_for_ctx(unsigned sched_ctx, struct starpu_task *task)
  86. {
  87. long long curr_total_instr = 0;
  88. /* long long curr_total_cycles = 0; */
  89. /* long long curr_total_cache_misses = 0; */
  90. /* long long curr_total_cache_refs = 0; */
  91. /* long long curr_total_branch_instr = 0; */
  92. long long curr_total_fps = 0;
  93. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx);
  94. struct starpu_sched_ctx_iterator it;
  95. int workerid;
  96. workers->init_iterator(workers, &it);
  97. while(workers->has_next(workers, &it))
  98. {
  99. ssize_t rread;
  100. workerid = workers->get_next(workers, &it);
  101. // Read event counter value
  102. struct read_format instr, /*cycles, cache_misses, cache_refs, branch_instr,*/ fps;
  103. rread = read(fd_instr[workerid], &instr, sizeof(struct read_format));
  104. assert(rread==sizeof(struct read_format));
  105. /* read(fd_cycles[workerid], &cycles, sizeof(long long)); */
  106. /* read(fd_cache_misses[workerid], &cache_misses, sizeof(long long)); */
  107. /* read(fd_cache_refs[workerid], &cache_refs, sizeof(long long)); */
  108. /* read(fd_branch_instr[workerid], &branch_instr, sizeof(long long)); */
  109. rread = read(fd_fps[workerid], &fps, sizeof(struct read_format));
  110. assert(rread == sizeof(struct read_format));
  111. curr_total_instr += (instr.time_enabled != 0 && instr.time_running !=0) ? instr.value * instr.time_enabled/instr.time_running : instr.value;
  112. printf("w%d instr time enabled %"PRIu64" time running %"PRIu64" \n", workerid, instr.time_enabled, instr.time_running);
  113. /* curr_total_cycles += cycles; */
  114. /* curr_total_cache_misses += cache_misses; */
  115. /* curr_total_cache_refs += cache_refs; */
  116. /* curr_total_branch_instr += branch_instr; */
  117. curr_total_fps += (fps.time_enabled != 0 && fps.time_running !=0) ? fps.value * fps.time_enabled/fps.time_running : fps.value;
  118. printf("w%d fps time enabled %lu time running %lu \n", workerid, fps.time_enabled, fps.time_running);
  119. }
  120. total_instr[sched_ctx] += curr_total_instr;
  121. /* total_cycles[sched_ctx] += curr_total_cycles; */
  122. /* total_cache_misses[sched_ctx] += curr_total_cache_misses; */
  123. /* total_cache_refs[sched_ctx] += curr_total_cache_refs; */
  124. /* total_branch_instr[sched_ctx] += curr_total_branch_instr; */
  125. total_fps[sched_ctx] += curr_total_fps;
  126. printf("%u: Instrs %lf k curr instr %lf k\n", sched_ctx, (double)total_instr[sched_ctx]/1000,
  127. (double)curr_total_instr/1000);
  128. printf("%u: Fps %lf k curr fps %lf k\n", sched_ctx,
  129. (double)total_fps[sched_ctx]/1000,
  130. (double)curr_total_fps/1000);
  131. printf("%u: Task Flops %lf k %s \n", sched_ctx, task->flops/1000, (task->cl && task->cl->model) ? task->cl->model->symbol : "task null");
  132. printf("-------------------------------------------\n");
  133. }
  134. void config_event(struct perf_event_attr *event, unsigned with_time, uint64_t event_type, uint64_t config_type)
  135. {
  136. memset(event, 0, sizeof(struct perf_event_attr));
  137. event->type = event_type;
  138. event->size = sizeof(struct perf_event_attr);
  139. event->config = config_type;
  140. event->disabled = 1; // Event is initially disabled
  141. event->exclude_kernel = 1; // excluding events that happen in the kernel space
  142. if(with_time)
  143. {
  144. /* if the PMU is multiplexing several events we measure the time spent to actually measure this event (time_running)
  145. and compare it to the one expected is did, thus we compute the precision of the counter*/
  146. event->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED|PERF_FORMAT_TOTAL_TIME_RUNNING;
  147. }
  148. }
  149. void open_event(int *file_desc, struct perf_event_attr *event, int group_fd)
  150. {
  151. *file_desc = perf_event_open(event, 0, -1, group_fd, 0);
  152. if (*file_desc == -1) {
  153. fprintf(stderr, "Error opening leader %llx\n", event->config);
  154. perror("perf_event_open");
  155. exit(0);
  156. }
  157. }
  158. void config_all_events_for_worker(int workerid)
  159. {
  160. config_event(&pe_instr[workerid], 1, PERF_TYPE_HARDWARE, PERF_COUNT_HW_INSTRUCTIONS);
  161. /* config_event(&pe_cycles[workerid], 0, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES); */
  162. /* config_event(&pe_cache_misses[workerid], 0, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_MISSES); */
  163. /* config_event(&pe_cache_refs[workerid], 0, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_REFERENCES); */
  164. /* config_event(&pe_branch_instr[workerid], 0, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_INSTRUCTIONS); */
  165. config_event(&pe_fps[workerid], 1, PERF_TYPE_RAW, 0x1010);
  166. }
  167. void open_all_events_for_worker(int curr_workerid)
  168. {
  169. open_event(&fd_instr[curr_workerid], &pe_instr[curr_workerid], -1);
  170. /* open_event(&fd_cycles[curr_workerid], &pe_cycles[curr_workerid], fd_instr[curr_workerid]); */
  171. /* open_event(&fd_cache_misses[curr_workerid], &pe_cache_misses[curr_workerid], fd_instr[curr_workerid]); */
  172. /* open_event(&fd_cache_refs[curr_workerid], &pe_cache_refs[curr_workerid], fd_instr[curr_workerid]); */
  173. /* open_event(&fd_branch_instr[curr_workerid], &pe_branch_instr[curr_workerid], fd_instr[curr_workerid]); */
  174. open_event(&fd_fps[curr_workerid], &pe_fps[curr_workerid], fd_instr[curr_workerid]);
  175. }
  176. void close_all_events_for_worker(int curr_workerid)
  177. {
  178. close(fd_instr[curr_workerid]);
  179. /* close(fd_cycles[curr_workerid]); */
  180. /* close(fd_cache_misses[curr_workerid]); */
  181. /* close(fd_cache_refs[curr_workerid]); */
  182. /* close(fd_branch_instr[curr_workerid]); */
  183. close(fd_fps[curr_workerid]);
  184. }
  185. void start_monitoring_all_events_for_worker(int workerid)
  186. {
  187. ioctl(fd_instr[workerid], PERF_EVENT_IOC_RESET, 0);
  188. ioctl(fd_instr[workerid], PERF_EVENT_IOC_ENABLE, 0);
  189. /* ioctl(fd_cycles[workerid], PERF_EVENT_IOC_RESET, 0); */
  190. /* ioctl(fd_cycles[workerid], PERF_EVENT_IOC_ENABLE, 0); */
  191. /* ioctl(fd_cache_misses[workerid], PERF_EVENT_IOC_RESET, 0); */
  192. /* ioctl(fd_cache_misses[workerid], PERF_EVENT_IOC_ENABLE, 0); */
  193. /* ioctl(fd_cache_refs[workerid], PERF_EVENT_IOC_RESET, 0); */
  194. /* ioctl(fd_cache_refs[workerid], PERF_EVENT_IOC_ENABLE, 0); */
  195. /* ioctl(fd_branch_instr[workerid], PERF_EVENT_IOC_RESET, 0); */
  196. /* ioctl(fd_branch_instr[workerid], PERF_EVENT_IOC_ENABLE, 0); */
  197. ioctl(fd_fps[workerid], PERF_EVENT_IOC_RESET, 0);
  198. ioctl(fd_fps[workerid], PERF_EVENT_IOC_ENABLE, 0);
  199. }
  200. void stop_monitoring_all_events_for_worker(int workerid)
  201. {
  202. ioctl(fd_instr[workerid], PERF_EVENT_IOC_DISABLE, 0);
  203. /* ioctl(fd_cycles[workerid], PERF_EVENT_IOC_DISABLE, 0); */
  204. /* ioctl(fd_cache_misses[workerid], PERF_EVENT_IOC_DISABLE, 0); */
  205. /* ioctl(fd_cache_refs[workerid], PERF_EVENT_IOC_DISABLE, 0); */
  206. /* ioctl(fd_branch_instr[workerid], PERF_EVENT_IOC_DISABLE, 0); */
  207. ioctl(fd_fps[workerid], PERF_EVENT_IOC_DISABLE, 0);
  208. }
  209. void perf_count_handle_idle_end(unsigned sched_ctx, int worker)
  210. {
  211. unsigned has_starpu_scheduler;
  212. unsigned has_awake_workers;
  213. has_starpu_scheduler = starpu_sched_ctx_has_starpu_scheduler(sched_ctx, &has_awake_workers);
  214. if(!has_starpu_scheduler && !has_awake_workers)
  215. {
  216. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx);
  217. struct starpu_sched_ctx_iterator it;
  218. int workerid;
  219. workers->init_iterator(workers, &it);
  220. while(workers->has_next(workers, &it))
  221. {
  222. workerid = workers->get_next(workers, &it);
  223. if(perf_event_opened[workerid])
  224. start_monitoring_all_events_for_worker(workerid);
  225. }
  226. }
  227. else
  228. {
  229. if(!perf_event_opened[worker])
  230. {
  231. config_all_events_for_worker(worker);
  232. open_all_events_for_worker(worker);
  233. perf_event_opened[worker] = 1;
  234. }
  235. start_monitoring_all_events_for_worker(worker);
  236. }
  237. }
  238. void perf_count_handle_poped_task(unsigned sched_ctx, int worker,
  239. struct starpu_task *task,
  240. __attribute__((unused))uint32_t footprint)
  241. {
  242. unsigned has_starpu_scheduler;
  243. unsigned has_awake_workers;
  244. has_starpu_scheduler = starpu_sched_ctx_has_starpu_scheduler(sched_ctx, &has_awake_workers);
  245. if(!has_starpu_scheduler && !has_awake_workers)
  246. {
  247. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx);
  248. struct starpu_sched_ctx_iterator it;
  249. int workerid;
  250. workers->init_iterator(workers, &it);
  251. while(workers->has_next(workers, &it))
  252. {
  253. workerid = workers->get_next(workers, &it);
  254. if(perf_event_opened[workerid])
  255. stop_monitoring_all_events_for_worker(workerid);
  256. }
  257. // printf("worker requesting %d in ctx %d \n", starpu_worker_get_id(), sched_ctx);
  258. print_results_for_ctx(sched_ctx, task);
  259. }
  260. else
  261. {
  262. if(perf_event_opened[worker])
  263. stop_monitoring_all_events_for_worker(worker);
  264. print_results_for_worker(worker, sched_ctx, task);
  265. }
  266. }
  267. void perf_count_init_worker(int workerid, unsigned sched_ctx)
  268. {
  269. if(!perf_event_opened[workerid])
  270. {
  271. open_all_events_for_worker(workerid);
  272. perf_event_opened[workerid] = 1;
  273. }
  274. else
  275. {
  276. close_all_events_for_worker(workerid);
  277. open_all_events_for_worker(workerid);
  278. }
  279. }
  280. void perf_count_start_ctx(unsigned sched_ctx)
  281. {
  282. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx);
  283. struct starpu_sched_ctx_iterator it;
  284. int workerid;
  285. workers->init_iterator(workers, &it);
  286. while(workers->has_next(workers, &it))
  287. {
  288. workerid = workers->get_next(workers, &it);
  289. config_all_events_for_worker(workerid);
  290. }
  291. }
  292. void perf_count_end_ctx(unsigned sched_ctx)
  293. {
  294. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx);
  295. struct starpu_sched_ctx_iterator it;
  296. int workerid;
  297. workers->init_iterator(workers, &it);
  298. while(workers->has_next(workers, &it))
  299. {
  300. workerid = workers->get_next(workers, &it);
  301. close_all_events_for_worker(workerid);
  302. }
  303. }
  304. struct sc_hypervisor_policy perf_count_policy =
  305. {
  306. .size_ctxs = NULL,
  307. .handle_poped_task = perf_count_handle_poped_task,
  308. .handle_pushed_task = NULL,
  309. .handle_idle_cycle = NULL,
  310. .handle_idle_end = perf_count_handle_idle_end,
  311. .handle_post_exec_hook = NULL,
  312. .handle_submitted_job = NULL,
  313. .end_ctx = perf_count_end_ctx,
  314. .start_ctx = perf_count_start_ctx,
  315. .init_worker = perf_count_init_worker,
  316. .custom = 0,
  317. .name = "perf_count"
  318. };