knobs.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2019 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. /* Performance counters and configurable knobs */
  17. #include <stdlib.h>
  18. #include <stdint.h>
  19. #include <starpu.h>
  20. #include <common/config.h>
  21. #include <common/starpu_spinlock.h>
  22. #include <core/workers.h>
  23. #include <common/knobs.h>
  24. /* Performance Monitoring */
  25. struct perf_counter_array
  26. {
  27. int size;
  28. struct starpu_perf_counter *array;
  29. int updater_array_size;
  30. void (**updater_array)(struct starpu_perf_counter_sample *sample, void *context);
  31. };
  32. static struct perf_counter_array global_counters = { .size = 0, .array = NULL, .updater_array_size = 0, .updater_array = NULL };
  33. static struct perf_counter_array per_worker_counters = { .size = 0, .array = NULL, .updater_array_size = 0, .updater_array = NULL };
  34. static struct perf_counter_array per_codelet_counters = { .size = 0, .array = NULL, .updater_array_size = 0, .updater_array = NULL };
  35. static struct starpu_perf_counter_sample global_sample = { .scope = starpu_perf_counter_scope_global, .listener = NULL, .value_array = NULL };
  36. /* - */
  37. void _starpu_perf_counter_sample_init(struct starpu_perf_counter_sample *sample, enum starpu_perf_counter_scope scope)
  38. {
  39. STARPU_ASSERT_PERF_COUNTER_SCOPE_DEFINED(scope);
  40. sample->scope = scope;
  41. sample->listener = NULL;
  42. sample->value_array = NULL;
  43. _starpu_spin_init(&sample->lock);
  44. }
  45. void _starpu_perf_counter_sample_exit(struct starpu_perf_counter_sample *sample)
  46. {
  47. STARPU_ASSERT(sample->listener == NULL);
  48. sample->listener = NULL;
  49. if (sample->value_array)
  50. {
  51. free(sample->value_array);
  52. }
  53. sample->value_array = NULL;
  54. sample->scope = starpu_perf_counter_scope_undefined;
  55. _starpu_spin_destroy(&sample->lock);
  56. }
  57. /* - */
  58. void _starpu_perf_counter_init(void)
  59. {
  60. STARPU_ASSERT(!_starpu_machine_is_running());
  61. _starpu_perf_counter_sample_init(&global_sample, starpu_perf_counter_scope_global);
  62. /* call counter registration routines in each modules */
  63. _starpu__task_c__register_counters();
  64. }
  65. void _starpu_perf_counter_exit(void)
  66. {
  67. STARPU_ASSERT(!_starpu_machine_is_running());
  68. _starpu_perf_counter_unregister_all_scopes();
  69. _starpu_perf_counter_sample_exit(&global_sample);
  70. }
  71. /* - */
  72. int starpu_perf_counter_scope_name_to_id(const char * const name)
  73. {
  74. if (strcmp(name, "global") == 0)
  75. return starpu_perf_counter_scope_global;
  76. if (strcmp(name, "per_worker") == 0)
  77. return starpu_perf_counter_scope_per_worker;
  78. if (strcmp(name, "per_codelet") == 0)
  79. return starpu_perf_counter_scope_per_codelet;
  80. return -1;
  81. }
  82. const char *starpu_perf_counter_scope_id_to_name(const enum starpu_perf_counter_scope scope)
  83. {
  84. switch (scope)
  85. {
  86. case starpu_perf_counter_scope_global:
  87. return "global";
  88. case starpu_perf_counter_scope_per_worker:
  89. return "per_worker";
  90. case starpu_perf_counter_scope_per_codelet:
  91. return "per_codelet";
  92. default:
  93. return NULL;
  94. };
  95. }
  96. /* - */
  97. int starpu_perf_counter_type_name_to_id(const char * const name)
  98. {
  99. if (strcmp(name, "int32") == 0)
  100. return starpu_perf_counter_type_int32;
  101. if (strcmp(name, "int64") == 0)
  102. return starpu_perf_counter_type_int64;
  103. if (strcmp(name, "float") == 0)
  104. return starpu_perf_counter_type_float;
  105. if (strcmp(name, "double") == 0)
  106. return starpu_perf_counter_type_double;
  107. return -1;
  108. }
  109. const char *starpu_perf_counter_type_id_to_name(const enum starpu_perf_counter_type type)
  110. {
  111. switch (type)
  112. {
  113. case starpu_perf_counter_type_int32:
  114. return "int32";
  115. case starpu_perf_counter_type_int64:
  116. return "int64";
  117. case starpu_perf_counter_type_float:
  118. return "float";
  119. case starpu_perf_counter_type_double:
  120. return "double";
  121. default:
  122. return NULL;
  123. };
  124. }
  125. static struct perf_counter_array *_get_counters(const enum starpu_perf_counter_scope scope)
  126. {
  127. STARPU_ASSERT_PERF_COUNTER_SCOPE_DEFINED(scope);
  128. switch (scope)
  129. {
  130. case starpu_perf_counter_scope_global:
  131. return &global_counters;
  132. case starpu_perf_counter_scope_per_worker:
  133. return &per_worker_counters;
  134. case starpu_perf_counter_scope_per_codelet:
  135. return &per_codelet_counters;
  136. default:
  137. STARPU_ABORT();
  138. };
  139. };
  140. /* - */
  141. int _starpu_perf_counter_register(enum starpu_perf_counter_scope scope, const char *name, enum starpu_perf_counter_type type, const char *help)
  142. {
  143. STARPU_ASSERT(!_starpu_machine_is_running());
  144. struct perf_counter_array * const counters = _get_counters(scope);
  145. STARPU_ASSERT_PERF_COUNTER_TYPE_DEFINED(type);
  146. const int index = counters->size++;
  147. _STARPU_REALLOC(counters->array, counters->size * sizeof(*counters->array));
  148. struct starpu_perf_counter * const new_counter = &counters->array[index];
  149. const int id = _starpu_perf_counter_id_build(scope, index);
  150. new_counter->id = id;
  151. new_counter->name = name;
  152. new_counter->help = help;
  153. new_counter->type = type;
  154. return id;
  155. }
  156. static void _unregister_counter_scope(enum starpu_perf_counter_scope scope)
  157. {
  158. STARPU_ASSERT(!_starpu_machine_is_running());
  159. struct perf_counter_array * const counters = _get_counters(scope);
  160. free(counters->array);
  161. counters->array = NULL;
  162. counters->size = 0;
  163. }
  164. void _starpu_perf_counter_unregister_all_scopes(void)
  165. {
  166. STARPU_ASSERT(!_starpu_machine_is_running());
  167. _unregister_counter_scope(starpu_perf_counter_scope_global);
  168. _unregister_counter_scope(starpu_perf_counter_scope_per_worker);
  169. _unregister_counter_scope(starpu_perf_counter_scope_per_codelet);
  170. }
  171. /* - */
  172. int starpu_perf_counter_nb(enum starpu_perf_counter_scope scope)
  173. {
  174. const struct perf_counter_array * const counters = _get_counters(scope);
  175. return counters->size;
  176. }
  177. int starpu_perf_counter_nth_to_id(enum starpu_perf_counter_scope scope, int nth)
  178. {
  179. return _starpu_perf_counter_id_build(scope, nth);
  180. }
  181. int starpu_perf_counter_name_to_id(enum starpu_perf_counter_scope scope, const char *name)
  182. {
  183. const struct perf_counter_array * const counters = _get_counters(scope);
  184. int index;
  185. for (index = 0; index < counters->size; index++)
  186. {
  187. if (strcmp(name, counters->array[index].name) == 0)
  188. {
  189. return _starpu_perf_counter_id_build(scope, index);
  190. }
  191. }
  192. return -1;
  193. }
  194. const char *starpu_perf_counter_id_to_name(int id)
  195. {
  196. const int scope = _starpu_perf_counter_id_get_scope(id);
  197. const int index = _starpu_perf_counter_id_get_index(id);
  198. const struct perf_counter_array * const counters = _get_counters(scope);
  199. if (index < 0 || index >= counters->size)
  200. return NULL;
  201. return counters->array[index].name;
  202. }
  203. const char *starpu_perf_counter_get_help_string(int id)
  204. {
  205. const int scope = _starpu_perf_counter_id_get_scope(id);
  206. const int index = _starpu_perf_counter_id_get_index(id);
  207. const struct perf_counter_array * const counters = _get_counters(scope);
  208. STARPU_ASSERT(index >= 0 && index < counters->size);
  209. return counters->array[index].help;
  210. }
  211. int starpu_perf_counter_get_type_id(int id)
  212. {
  213. const int scope = _starpu_perf_counter_id_get_scope(id);
  214. const int index = _starpu_perf_counter_id_get_index(id);
  215. const struct perf_counter_array * const counters = _get_counters(scope);
  216. STARPU_ASSERT(index >= 0 && index < counters->size);
  217. return counters->array[index].type;
  218. }
  219. /* - */
  220. void starpu_perf_counter_list_avail(enum starpu_perf_counter_scope scope)
  221. {
  222. const struct perf_counter_array * const counters = _get_counters(scope);
  223. int index;
  224. for (index = 0; index < counters->size; index++)
  225. {
  226. const struct starpu_perf_counter * const counter = &counters->array[index];
  227. printf("0x%08x:%s [%s] - %s\n", _starpu_perf_counter_id_build(scope, index), counter->name, starpu_perf_counter_type_id_to_name(counter->type), counter->help);
  228. }
  229. }
  230. void starpu_perf_counter_list_all_avail(void)
  231. {
  232. printf("scope: global\n");
  233. starpu_perf_counter_list_avail(starpu_perf_counter_scope_global);
  234. printf("scope: per_worker\n");
  235. starpu_perf_counter_list_avail(starpu_perf_counter_scope_per_worker);
  236. printf("scope: per_codelet\n");
  237. starpu_perf_counter_list_avail(starpu_perf_counter_scope_per_codelet);
  238. }
  239. /* - */
  240. struct starpu_perf_counter_set *starpu_perf_counter_set_alloc(enum starpu_perf_counter_scope scope)
  241. {
  242. struct perf_counter_array *counters = _get_counters(scope);
  243. struct starpu_perf_counter_set *set;
  244. _STARPU_MALLOC(set, sizeof(*set));
  245. set->scope = scope;
  246. set->size = counters->size;
  247. _STARPU_CALLOC(set->index_array, set->size, sizeof(*set->index_array));
  248. return set;
  249. }
  250. void starpu_perf_counter_set_free(struct starpu_perf_counter_set *set)
  251. {
  252. memset(set->index_array, 0, set->size*sizeof(*set->index_array));
  253. free(set->index_array);
  254. memset(set, 0, sizeof(*set));
  255. free(set);
  256. }
  257. /* - */
  258. void starpu_perf_counter_set_enable_id(struct starpu_perf_counter_set *set, int id)
  259. {
  260. const int index = _starpu_perf_counter_id_get_index(id);
  261. STARPU_ASSERT(index >= 0 && index < set->size);
  262. set->index_array[index] = 1;
  263. }
  264. void starpu_perf_counter_set_disable_id(struct starpu_perf_counter_set *set, int id)
  265. {
  266. const int index = _starpu_perf_counter_id_get_index(id);
  267. STARPU_ASSERT(index >= 0 && index < set->size);
  268. set->index_array[index] = 0;
  269. }
  270. /* - */
  271. struct starpu_perf_counter_listener *starpu_perf_counter_listener_init(struct starpu_perf_counter_set *set,
  272. void (*callback)(struct starpu_perf_counter_listener *listener, struct starpu_perf_counter_sample *sample, void *context),
  273. void *user_arg)
  274. {
  275. struct starpu_perf_counter_listener *listener;
  276. _STARPU_MALLOC(listener, sizeof(*listener));
  277. listener->set = set;
  278. listener->callback = callback;
  279. listener->user_arg = user_arg;
  280. return listener;
  281. }
  282. void starpu_perf_counter_listener_exit(struct starpu_perf_counter_listener *listener)
  283. {
  284. memset(listener, 0, sizeof(*listener));
  285. free(listener);
  286. }
  287. /* - */
  288. static void set_listener(struct starpu_perf_counter_sample *sample, struct starpu_perf_counter_listener *listener)
  289. {
  290. _starpu_spin_lock(&sample->lock);
  291. STARPU_ASSERT(sample->listener == NULL);
  292. STARPU_ASSERT(listener->set != NULL);
  293. STARPU_ASSERT(listener->set->scope == sample->scope);
  294. sample->listener = listener;
  295. /* Assume a single listener, for now, which sets the set of counters to monitor */
  296. STARPU_ASSERT(sample->value_array == NULL);
  297. _STARPU_CALLOC(sample->value_array, sample->listener->set->size, sizeof(*sample->value_array));
  298. _starpu_spin_unlock(&sample->lock);
  299. }
  300. void starpu_perf_counter_set_global_listener(struct starpu_perf_counter_listener *listener)
  301. {
  302. set_listener(&global_sample, listener);
  303. }
  304. void starpu_perf_counter_set_per_worker_listener(unsigned workerid, struct starpu_perf_counter_listener *listener)
  305. {
  306. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  307. set_listener(&worker->perf_counter_sample, listener);
  308. }
  309. void starpu_perf_counter_set_all_per_worker_listeners(struct starpu_perf_counter_listener *listener)
  310. {
  311. unsigned nworkers = _starpu_worker_get_count();
  312. unsigned workerid;
  313. for (workerid = 0; workerid < nworkers; workerid++)
  314. {
  315. starpu_perf_counter_set_per_worker_listener(workerid, listener);
  316. }
  317. }
  318. void starpu_perf_counter_set_per_codelet_listener(struct starpu_codelet *cl, struct starpu_perf_counter_listener *listener)
  319. {
  320. STARPU_ASSERT(cl->perf_counter_values == NULL);
  321. _STARPU_CALLOC(cl->perf_counter_values, 1, sizeof(*cl->perf_counter_values));
  322. STARPU_ASSERT(cl->perf_counter_sample == NULL);
  323. _STARPU_MALLOC(cl->perf_counter_sample, sizeof(*cl->perf_counter_sample));
  324. _starpu_perf_counter_sample_init(cl->perf_counter_sample, starpu_perf_counter_scope_per_codelet);
  325. set_listener(cl->perf_counter_sample, listener);
  326. }
  327. /* - */
  328. void unset_listener(struct starpu_perf_counter_sample *sample)
  329. {
  330. _starpu_spin_lock(&sample->lock);
  331. STARPU_ASSERT(sample->listener != NULL);
  332. memset(sample->value_array, 0, sample->listener->set->size * sizeof(*sample->value_array));
  333. free(sample->value_array);
  334. sample->value_array = NULL;
  335. sample->listener = NULL;
  336. _starpu_spin_unlock(&sample->lock);
  337. }
  338. void starpu_perf_counter_unset_global_listener()
  339. {
  340. unset_listener(&global_sample);
  341. }
  342. void starpu_perf_counter_unset_per_worker_listener(unsigned workerid)
  343. {
  344. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  345. unset_listener(&worker->perf_counter_sample);
  346. }
  347. void starpu_perf_counter_unset_all_per_worker_listeners(void)
  348. {
  349. unsigned nworkers = _starpu_worker_get_count();
  350. unsigned workerid;
  351. for (workerid = 0; workerid < nworkers; workerid++)
  352. {
  353. starpu_perf_counter_unset_per_worker_listener(workerid);
  354. }
  355. }
  356. void starpu_perf_counter_unset_per_codelet_listener(struct starpu_codelet *cl)
  357. {
  358. STARPU_ASSERT(cl->perf_counter_sample != NULL);
  359. unset_listener(cl->perf_counter_sample);
  360. _starpu_perf_counter_sample_exit(cl->perf_counter_sample);
  361. free(cl->perf_counter_sample);
  362. cl->perf_counter_sample = NULL;
  363. free(cl->perf_counter_values);
  364. cl->perf_counter_values = NULL;
  365. }
  366. /* - */
  367. void _starpu_perf_counter_register_updater(enum starpu_perf_counter_scope scope, void (*updater)(struct starpu_perf_counter_sample *sample, void *context))
  368. {
  369. STARPU_ASSERT(!_starpu_machine_is_running());
  370. struct perf_counter_array *counters = _get_counters(scope);
  371. int upd_id;
  372. upd_id = counters->updater_array_size++;
  373. _STARPU_REALLOC(counters->updater_array, counters->updater_array_size * sizeof(*counters->updater_array));
  374. counters->updater_array[upd_id] = updater;
  375. }
  376. /* - */
  377. static void update_sample(struct starpu_perf_counter_sample *sample, void *context)
  378. {
  379. _starpu_spin_lock(&sample->lock);
  380. struct perf_counter_array *counters = _get_counters(sample->scope);
  381. /* for now, we assume that a sample will only be updated if it has a listener plugged, with a non-empty set */
  382. if (sample->listener != NULL && sample->listener->set != NULL)
  383. {
  384. if (counters->updater_array_size > 0)
  385. {
  386. int upd_id;
  387. for (upd_id = 0; upd_id < counters->updater_array_size; upd_id++)
  388. {
  389. counters->updater_array[upd_id](sample, context);
  390. }
  391. if (sample->listener != NULL)
  392. {
  393. sample->listener->callback(sample->listener, sample, context);
  394. }
  395. }
  396. }
  397. _starpu_spin_unlock(&sample->lock);
  398. }
  399. void _starpu_perf_counter_update_global_sample(void)
  400. {
  401. update_sample(&global_sample, NULL);
  402. }
  403. void _starpu_perf_counter_update_per_worker_sample(unsigned workerid)
  404. {
  405. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  406. update_sample(&worker->perf_counter_sample, worker);
  407. }
  408. void _starpu_perf_counter_update_per_codelet_sample(struct starpu_codelet *cl)
  409. {
  410. update_sample(cl->perf_counter_sample, cl);
  411. }
  412. #define STARPU_PERF_COUNTER_SAMPLE_GET_TYPED_VALUE(STRING, TYPE) \
  413. TYPE starpu_perf_counter_sample_get_##STRING##_value(struct starpu_perf_counter_sample *sample, const int counter_id) \
  414. { \
  415. STARPU_ASSERT(starpu_perf_counter_get_type_id(counter_id) == starpu_perf_counter_type_##STRING); \
  416. STARPU_ASSERT(sample->listener != NULL && sample->listener->set != NULL); \
  417. STARPU_ASSERT(_starpu_perf_counter_id_get_scope(counter_id) == sample->listener->set->scope); \
  418. \
  419. const struct starpu_perf_counter_set * const set = sample->listener->set; \
  420. const int index = _starpu_perf_counter_id_get_index(counter_id); \
  421. STARPU_ASSERT(index < set->size); \
  422. STARPU_ASSERT(set->index_array[index] > 0); \
  423. return sample->value_array[index].STRING##_val; \
  424. }
  425. STARPU_PERF_COUNTER_SAMPLE_GET_TYPED_VALUE(int32, int32_t);
  426. STARPU_PERF_COUNTER_SAMPLE_GET_TYPED_VALUE(int64, int64_t);
  427. STARPU_PERF_COUNTER_SAMPLE_GET_TYPED_VALUE(float, float);
  428. STARPU_PERF_COUNTER_SAMPLE_GET_TYPED_VALUE(double, double);
  429. #undef STARPU_PERF_COUNTER_SAMPLE_GET_TYPED_VALUE
  430. /* -------------------------------------------------------------------- */
  431. /* Performance Steering */
  432. struct perf_knob_array
  433. {
  434. int size;
  435. struct starpu_perf_knob *array;
  436. };
  437. static struct perf_knob_array global_knobs = { .size = 0, .array = NULL };
  438. static struct perf_knob_array per_worker_knobs = { .size = 0, .array = NULL };
  439. static struct perf_knob_array per_scheduler_knobs = { .size = 0, .array = NULL };
  440. void _starpu_perf_knob_init(void)
  441. {
  442. STARPU_ASSERT(!_starpu_machine_is_running());
  443. /* call knob registration routines in each modules */
  444. _starpu__workers_c__register_knobs();
  445. _starpu__task_c__register_knobs();
  446. _starpu__dmda_c__register_knobs();
  447. }
  448. void _starpu_perf_knob_exit(void)
  449. {
  450. STARPU_ASSERT(!_starpu_machine_is_running());
  451. _starpu_perf_knob_unregister_all_scopes();
  452. }
  453. /* - */
  454. int starpu_perf_knob_scope_name_to_id(const char * const name)
  455. {
  456. if (strcmp(name, "global") == 0)
  457. return starpu_perf_knob_scope_global;
  458. if (strcmp(name, "per_worker") == 0)
  459. return starpu_perf_knob_scope_per_worker;
  460. if (strcmp(name, "per_scheduler") == 0)
  461. return starpu_perf_knob_scope_per_scheduler;
  462. return -1;
  463. }
  464. const char *starpu_perf_knob_scope_id_to_name(const enum starpu_perf_knob_scope scope)
  465. {
  466. switch (scope)
  467. {
  468. case starpu_perf_knob_scope_global:
  469. return "global";
  470. case starpu_perf_knob_scope_per_worker:
  471. return "per_worker";
  472. case starpu_perf_knob_scope_per_scheduler:
  473. return "per_scheduler";
  474. default:
  475. return NULL;
  476. };
  477. }
  478. /* - */
  479. int starpu_perf_knob_type_name_to_id(const char * const name)
  480. {
  481. if (strcmp(name, "int32") == 0)
  482. return starpu_perf_knob_type_int32;
  483. if (strcmp(name, "int64") == 0)
  484. return starpu_perf_knob_type_int64;
  485. if (strcmp(name, "float") == 0)
  486. return starpu_perf_knob_type_float;
  487. if (strcmp(name, "double") == 0)
  488. return starpu_perf_knob_type_double;
  489. return -1;
  490. }
  491. const char *starpu_perf_knob_type_id_to_name(const enum starpu_perf_knob_type type)
  492. {
  493. switch (type)
  494. {
  495. case starpu_perf_knob_type_int32:
  496. return "int32";
  497. case starpu_perf_knob_type_int64:
  498. return "int64";
  499. case starpu_perf_knob_type_float:
  500. return "float";
  501. case starpu_perf_knob_type_double:
  502. return "double";
  503. default:
  504. return NULL;
  505. };
  506. }
  507. static struct perf_knob_array *_get_knobs(const enum starpu_perf_knob_scope scope)
  508. {
  509. STARPU_ASSERT_PERF_KNOB_SCOPE_DEFINED(scope);
  510. switch (scope)
  511. {
  512. case starpu_perf_knob_scope_global:
  513. return &global_knobs;
  514. case starpu_perf_knob_scope_per_worker:
  515. return &per_worker_knobs;
  516. case starpu_perf_knob_scope_per_scheduler:
  517. return &per_scheduler_knobs;
  518. default:
  519. STARPU_ABORT();
  520. };
  521. };
  522. /* - */
  523. struct starpu_perf_knob_group *_starpu_perf_knob_group_register(
  524. enum starpu_perf_knob_scope scope,
  525. void (*set_func)(const struct starpu_perf_knob * const knob, void *context, const struct starpu_perf_knob_value * const value),
  526. void (*get_func)(const struct starpu_perf_knob * const knob, void *context, struct starpu_perf_knob_value * const value))
  527. {
  528. STARPU_ASSERT_PERF_KNOB_SCOPE_DEFINED(scope);
  529. STARPU_ASSERT(set_func != NULL);
  530. STARPU_ASSERT(get_func != NULL);
  531. struct starpu_perf_knob_group *new_group;
  532. _STARPU_MALLOC(new_group, sizeof(*new_group));
  533. new_group->scope = scope;
  534. new_group->set = set_func;
  535. new_group->get = get_func;
  536. new_group->array_size = 0;
  537. new_group->array = NULL;
  538. return new_group;
  539. }
  540. void _starpu_perf_knob_group_unregister(struct starpu_perf_knob_group *group)
  541. {
  542. STARPU_ASSERT((group->array_size > 0 && group->array != NULL) || (group->array_size = 0 && group->array == NULL));
  543. if (group->array != NULL)
  544. {
  545. free(group->array);
  546. }
  547. memset(group, 0, sizeof(*group));
  548. }
  549. /* - */
  550. int _starpu_perf_knob_register(struct starpu_perf_knob_group *group, const char *name, enum starpu_perf_knob_type type, const char *help)
  551. {
  552. STARPU_ASSERT(!_starpu_machine_is_running());
  553. struct perf_knob_array * const knobs = _get_knobs(group->scope);
  554. STARPU_ASSERT_PERF_KNOB_TYPE_DEFINED(type);
  555. const int index = knobs->size++;
  556. _STARPU_REALLOC(knobs->array, knobs->size * sizeof(*knobs->array));
  557. struct starpu_perf_knob * const new_knob = &knobs->array[index];
  558. const int id = _starpu_perf_knob_id_build(group->scope, index);
  559. new_knob->id = id;
  560. new_knob->name = name;
  561. new_knob->help = help;
  562. new_knob->type = type;
  563. new_knob->group = group;
  564. new_knob->id_in_group = group->array_size++;
  565. _STARPU_REALLOC(group->array, group->array_size * sizeof(*group->array));
  566. group->array[new_knob->id_in_group] = new_knob;
  567. return id;
  568. }
  569. static void _unregister_knob_scope(enum starpu_perf_knob_scope scope)
  570. {
  571. STARPU_ASSERT(!_starpu_machine_is_running());
  572. struct perf_knob_array * const knobs = _get_knobs(scope);
  573. free(knobs->array);
  574. knobs->array = NULL;
  575. knobs->size = 0;
  576. }
  577. void _starpu_perf_knob_unregister_all_scopes(void)
  578. {
  579. STARPU_ASSERT(!_starpu_machine_is_running());
  580. _unregister_knob_scope(starpu_perf_knob_scope_global);
  581. _unregister_knob_scope(starpu_perf_knob_scope_per_worker);
  582. _unregister_knob_scope(starpu_perf_knob_scope_per_scheduler);
  583. }
  584. /* - */
  585. int starpu_perf_knob_nb(enum starpu_perf_knob_scope scope)
  586. {
  587. const struct perf_knob_array * const knobs = _get_knobs(scope);
  588. return knobs->size;
  589. }
  590. int starpu_perf_knob_nth_to_id(enum starpu_perf_knob_scope scope, int nth)
  591. {
  592. return _starpu_perf_knob_id_build(scope, nth);
  593. }
  594. int starpu_perf_knob_name_to_id(enum starpu_perf_knob_scope scope, const char *name)
  595. {
  596. const struct perf_knob_array * const knobs = _get_knobs(scope);
  597. int index;
  598. for (index = 0; index < knobs->size; index++)
  599. {
  600. if (strcmp(name, knobs->array[index].name) == 0)
  601. {
  602. return _starpu_perf_knob_id_build(scope, index);
  603. }
  604. }
  605. return -1;
  606. }
  607. const char *starpu_perf_knob_id_to_name(int id)
  608. {
  609. const int scope = _starpu_perf_knob_id_get_scope(id);
  610. const int index = _starpu_perf_knob_id_get_index(id);
  611. const struct perf_knob_array * const knobs = _get_knobs(scope);
  612. if (index < 0 || index >= knobs->size)
  613. return NULL;
  614. return knobs->array[index].name;
  615. }
  616. const char *starpu_perf_knob_get_help_string(int id)
  617. {
  618. const int scope = _starpu_perf_knob_id_get_scope(id);
  619. const int index = _starpu_perf_knob_id_get_index(id);
  620. const struct perf_knob_array * const knobs = _get_knobs(scope);
  621. STARPU_ASSERT(index >= 0 && index < knobs->size);
  622. return knobs->array[index].help;
  623. }
  624. int starpu_perf_knob_get_type_id(int id)
  625. {
  626. const int scope = _starpu_perf_knob_id_get_scope(id);
  627. const int index = _starpu_perf_knob_id_get_index(id);
  628. const struct perf_knob_array * const knobs = _get_knobs(scope);
  629. STARPU_ASSERT(index >= 0 && index < knobs->size);
  630. return knobs->array[index].type;
  631. }
  632. static struct starpu_perf_knob *get_knob(int id)
  633. {
  634. const int scope = _starpu_perf_knob_id_get_scope(id);
  635. struct perf_knob_array *knobs = _get_knobs(scope);
  636. const int index = _starpu_perf_knob_id_get_index(id);
  637. STARPU_ASSERT(index >= 0 && index < knobs->size);
  638. return &knobs->array[index];
  639. }
  640. /* - */
  641. void starpu_perf_knob_list_avail(enum starpu_perf_knob_scope scope)
  642. {
  643. const struct perf_knob_array * const knobs = _get_knobs(scope);
  644. int index;
  645. for (index = 0; index < knobs->size; index++)
  646. {
  647. const struct starpu_perf_knob * const knob = &knobs->array[index];
  648. printf("0x%08x:%s [%s] - %s\n", _starpu_perf_knob_id_build(scope, index), knob->name, starpu_perf_knob_type_id_to_name(knob->type), knob->help);
  649. }
  650. }
  651. void starpu_perf_knob_list_all_avail(void)
  652. {
  653. printf("scope: global\n");
  654. starpu_perf_knob_list_avail(starpu_perf_knob_scope_global);
  655. printf("scope: per_worker\n");
  656. starpu_perf_knob_list_avail(starpu_perf_knob_scope_per_worker);
  657. printf("scope: per_scheduler\n");
  658. starpu_perf_knob_list_avail(starpu_perf_knob_scope_per_scheduler);
  659. }
  660. #define __STARPU_PERF_KNOB_SET_TYPED_VALUE(SCOPE_NAME, STRING, TYPE) \
  661. void starpu_perf_knob_set_##SCOPE_NAME##_##STRING##_value(const int knob_id, const TYPE value) \
  662. { \
  663. STARPU_ASSERT(_starpu_perf_knob_id_get_scope(knob_id) == starpu_perf_knob_scope_global); \
  664. const struct starpu_perf_knob * const knob = get_knob(knob_id); \
  665. STARPU_ASSERT(starpu_perf_knob_get_type_id(knob_id) == starpu_perf_knob_type_##STRING); \
  666. const struct starpu_perf_knob_group * const knob_group = knob->group; \
  667. const struct starpu_perf_knob_value kv = { .val_##TYPE = value }; \
  668. knob_group->set(knob, NULL, &kv); \
  669. }
  670. __STARPU_PERF_KNOB_SET_TYPED_VALUE(global, int32, int32_t);
  671. __STARPU_PERF_KNOB_SET_TYPED_VALUE(global, int64, int64_t);
  672. __STARPU_PERF_KNOB_SET_TYPED_VALUE(global, float, float);
  673. __STARPU_PERF_KNOB_SET_TYPED_VALUE(global, double, double);
  674. #undef __STARPU_PERF_KNOB_SAMPLE_SET_TYPED_VALUE
  675. #define __STARPU_PERF_KNOB_GET_TYPED_VALUE(SCOPE_NAME, STRING, TYPE) \
  676. TYPE starpu_perf_knob_get_##SCOPE_NAME##_##STRING##_value(const int knob_id) \
  677. { \
  678. STARPU_ASSERT(_starpu_perf_knob_id_get_scope(knob_id) == starpu_perf_knob_scope_global); \
  679. const struct starpu_perf_knob * const knob = get_knob(knob_id); \
  680. STARPU_ASSERT(starpu_perf_knob_get_type_id(knob_id) == starpu_perf_knob_type_##STRING); \
  681. const struct starpu_perf_knob_group * const knob_group = knob->group; \
  682. struct starpu_perf_knob_value kv; \
  683. knob_group->get(knob, NULL, &kv); \
  684. return kv.val_##TYPE; \
  685. }
  686. __STARPU_PERF_KNOB_GET_TYPED_VALUE(global, int32, int32_t);
  687. __STARPU_PERF_KNOB_GET_TYPED_VALUE(global, int64, int64_t);
  688. __STARPU_PERF_KNOB_GET_TYPED_VALUE(global, float, float);
  689. __STARPU_PERF_KNOB_GET_TYPED_VALUE(global, double, double);
  690. #undef __STARPU_PERF_KNOB_SAMPLE_GET_TYPED_VALUE
  691. #define __STARPU_PERF_KNOB_SET_TYPED_VALUE_WITH_CONTEXT(SCOPE_NAME, STRING, TYPE, CONTEXT_TYPE, CONTEXT_VAR) \
  692. void starpu_perf_knob_set_##SCOPE_NAME##_##STRING##_value(const int knob_id, CONTEXT_TYPE CONTEXT_VAR, const TYPE value) \
  693. { \
  694. STARPU_ASSERT(_starpu_perf_knob_id_get_scope(knob_id) == starpu_perf_knob_scope_##SCOPE_NAME); \
  695. const struct starpu_perf_knob * const knob = get_knob(knob_id); \
  696. STARPU_ASSERT(starpu_perf_knob_get_type_id(knob_id) == starpu_perf_knob_type_##STRING); \
  697. const struct starpu_perf_knob_group * const knob_group = knob->group; \
  698. const struct starpu_perf_knob_value kv = { .val_##TYPE = value }; \
  699. knob_group->set(knob, &CONTEXT_VAR, &kv); \
  700. }
  701. __STARPU_PERF_KNOB_SET_TYPED_VALUE_WITH_CONTEXT(per_worker, int32, int32_t, unsigned, workerid);
  702. __STARPU_PERF_KNOB_SET_TYPED_VALUE_WITH_CONTEXT(per_worker, int64, int64_t, unsigned, workerid);
  703. __STARPU_PERF_KNOB_SET_TYPED_VALUE_WITH_CONTEXT(per_worker, float, float, unsigned, workerid);
  704. __STARPU_PERF_KNOB_SET_TYPED_VALUE_WITH_CONTEXT(per_worker, double, double, unsigned, workerid);
  705. __STARPU_PERF_KNOB_SET_TYPED_VALUE_WITH_CONTEXT(per_scheduler, int32, int32_t, const char *, sched_policy_name);
  706. __STARPU_PERF_KNOB_SET_TYPED_VALUE_WITH_CONTEXT(per_scheduler, int64, int64_t, const char *, sched_policy_name);
  707. __STARPU_PERF_KNOB_SET_TYPED_VALUE_WITH_CONTEXT(per_scheduler, float, float, const char *, sched_policy_name);
  708. __STARPU_PERF_KNOB_SET_TYPED_VALUE_WITH_CONTEXT(per_scheduler, double, double, const char *, sched_policy_name);
  709. #undef __STARPU_PERF_KNOB_SAMPLE_SET_TYPED_VALUE_WITH_CONTEXT
  710. #define __STARPU_PERF_KNOB_GET_TYPED_VALUE_WITH_CONTEXT(SCOPE_NAME, STRING, TYPE, CONTEXT_TYPE, CONTEXT_VAR) \
  711. TYPE starpu_perf_knob_get_##SCOPE_NAME##_##STRING##_value(const int knob_id, CONTEXT_TYPE CONTEXT_VAR) \
  712. { \
  713. STARPU_ASSERT(_starpu_perf_knob_id_get_scope(knob_id) == starpu_perf_knob_scope_##SCOPE_NAME); \
  714. const struct starpu_perf_knob * const knob = get_knob(knob_id); \
  715. STARPU_ASSERT(starpu_perf_knob_get_type_id(knob_id) == starpu_perf_knob_type_##STRING); \
  716. const struct starpu_perf_knob_group * const knob_group = knob->group; \
  717. struct starpu_perf_knob_value kv; \
  718. knob_group->get(knob, &CONTEXT_VAR, &kv); \
  719. return kv.val_##TYPE; \
  720. }
  721. __STARPU_PERF_KNOB_GET_TYPED_VALUE_WITH_CONTEXT(per_worker, int32, int32_t, unsigned, workerid);
  722. __STARPU_PERF_KNOB_GET_TYPED_VALUE_WITH_CONTEXT(per_worker, int64, int64_t, unsigned, workerid);
  723. __STARPU_PERF_KNOB_GET_TYPED_VALUE_WITH_CONTEXT(per_worker, float, float, unsigned, workerid);
  724. __STARPU_PERF_KNOB_GET_TYPED_VALUE_WITH_CONTEXT(per_worker, double, double, unsigned, workerid);
  725. __STARPU_PERF_KNOB_GET_TYPED_VALUE_WITH_CONTEXT(per_scheduler, int32, int32_t, const char *, sched_policy_name);
  726. __STARPU_PERF_KNOB_GET_TYPED_VALUE_WITH_CONTEXT(per_scheduler, int64, int64_t, const char *, sched_policy_name);
  727. __STARPU_PERF_KNOB_GET_TYPED_VALUE_WITH_CONTEXT(per_scheduler, float, float, const char *, sched_policy_name);
  728. __STARPU_PERF_KNOB_GET_TYPED_VALUE_WITH_CONTEXT(per_scheduler, double, double, const char *, sched_policy_name);
  729. #undef __STARPU_PERF_KNOB_SAMPLE_GET_TYPED_VALUE_WITH_CONTEXT