starpu_top.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011 William Braik, Yann Courtois, Jean-Marie Couteyen, Anthony Roy
  4. * Copyright (C) 2011, 2012, 2013 CNRS
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <starpu_top.h>
  18. #include <top/starpu_top_message_queue.h>
  19. #include <top/starpu_top_connection.h>
  20. #include <top/starpu_top_core.h>
  21. #include <profiling/profiling.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <math.h>
  25. #include <common/timing.h>
  26. #include <common/thread.h>
  27. #include <common/config.h>
  28. extern struct _starpu_top_message_queue* _starpu_top_mt;
  29. int _starpu_top = 0;
  30. int starpu_top_debug_on = 0;
  31. unsigned int starpu_top_data_cpt = 0;
  32. unsigned int starpu_top_param_cpt = 0;
  33. struct starpu_top_data* starpu_top_first_data = NULL;
  34. struct starpu_top_param* starpu_top_first_param = NULL;
  35. struct starpu_top_data** starpu_top_datas;
  36. struct starpu_top_param** starpu_top_params;
  37. sem_t starpu_top_wait_for_go;
  38. starpu_pthread_mutex_t starpu_top_wait_for_continue_mutex;
  39. starpu_pthread_cond_t starpu_top_wait_for_continue_cond = STARPU_PTHREAD_COND_INITIALIZER;
  40. #undef _starpu_top_status_get
  41. int _starpu_top_status_get(void)
  42. {
  43. return _starpu_top;
  44. }
  45. static
  46. unsigned long long current_timestamp(void);
  47. /*********************************************
  48. *****************INIT FUNC********************
  49. **********************************************/
  50. static
  51. char *message_for_topdata_init(struct starpu_top_data* data);
  52. static
  53. char *message_for_topparam_init(struct starpu_top_param* param);
  54. /*
  55. * we store data and param in a tab to offer a O(1) access when the program is
  56. * running
  57. */
  58. static
  59. void copy_data_and_param(void)
  60. {
  61. printf("%s:%d trace\n", __FILE__, __LINE__);
  62. //copying datas
  63. starpu_top_datas = (struct starpu_top_data **) malloc(starpu_top_data_cpt*sizeof(struct starpu_top_data*));
  64. struct starpu_top_data* cur = starpu_top_first_data;
  65. unsigned int i = 0;
  66. for(i = 0; i < starpu_top_data_cpt; i++)
  67. {
  68. starpu_top_datas[i] = cur;
  69. cur = cur->next;
  70. }
  71. //copying params
  72. starpu_top_params = (struct starpu_top_param **) malloc(starpu_top_param_cpt*sizeof(struct starpu_top_param*));
  73. struct starpu_top_param* cur2 = starpu_top_first_param;
  74. for(i = 0; i < starpu_top_param_cpt; i++)
  75. {
  76. starpu_top_params[i] = cur2;
  77. cur2 = cur2->next;
  78. }
  79. }
  80. static void starpu_top_get_device_type(int id, char* type)
  81. {
  82. enum starpu_worker_archtype device_type=starpu_worker_get_type(id);
  83. switch (device_type)
  84. {
  85. case STARPU_CPU_WORKER:
  86. strncpy(type, "CPU",9);
  87. break;
  88. case STARPU_CUDA_WORKER:
  89. strncpy(type, "CUDA",9);
  90. break;
  91. case STARPU_OPENCL_WORKER:
  92. strncpy(type, "OPENCL",9);
  93. break;
  94. case STARPU_ANY_WORKER:
  95. strncpy(type, "ANY",9);
  96. break;
  97. case STARPU_MIC_WORKER:
  98. strncpy(type, "MIC", 9);
  99. break;
  100. case STARPU_SCC_WORKER:
  101. strncpy(type, "SCC", 9);
  102. break;
  103. }
  104. type[9] = 0;
  105. }
  106. static void starpu_top_send_devices_info(void)
  107. {
  108. char* message=(char *)malloc(5*sizeof(char));
  109. snprintf(message,5,"DEV\n");
  110. _starpu_top_message_add(_starpu_top_mt,message);
  111. unsigned int i;
  112. for(i=0;i<starpu_worker_get_count();i++)
  113. {
  114. message=(char *)malloc(sizeof(char)*128);
  115. char dev_type[10];
  116. char dev_name[64];
  117. starpu_top_get_device_type(i,dev_type);
  118. starpu_worker_get_name(i, dev_name,64);
  119. snprintf(message, 128, "%u;%s;%s\n", i, dev_type, dev_name);
  120. _starpu_top_message_add(_starpu_top_mt,message);
  121. }
  122. message=(char*)malloc(6*sizeof(char));
  123. snprintf(message,6,"/DEV\n");
  124. _starpu_top_message_add(_starpu_top_mt,message);
  125. }
  126. void starpu_top_init_and_wait(const char* server_name)
  127. {
  128. _starpu_top=1;
  129. sem_init(&starpu_top_wait_for_go,0,0);
  130. STARPU_PTHREAD_MUTEX_INIT(&starpu_top_wait_for_continue_mutex, NULL);
  131. //profiling activation
  132. starpu_profiling_status_set(STARPU_PROFILING_ENABLE);
  133. //init locked queue before adding the first message
  134. _starpu_top_mt = _starpu_top_message_queue_new();
  135. STARPU_ASSERT(_starpu_top_mt);
  136. //waiting for UI to connect
  137. printf("%s:%d launching network threads\n", __FILE__, __LINE__);
  138. _starpu_top_communications_threads_launcher();
  139. //sending server information (report to protocol)
  140. char* message = (char *) malloc(strlen("SERVERINFO\n")+1);
  141. sprintf(message, "%s", "SERVERINFO\n");
  142. _starpu_top_message_add(_starpu_top_mt,message);
  143. message = (char *) malloc(strlen(server_name)+2);
  144. sprintf(message, "%s\n", server_name);
  145. _starpu_top_message_add(_starpu_top_mt,message);
  146. message = (char *) malloc(25);
  147. sprintf(message, "%llu\n", current_timestamp());
  148. _starpu_top_message_add(_starpu_top_mt,message);
  149. message = (char *) malloc(strlen("/SERVERINFO\n")+1);
  150. sprintf(message,"%s", "/SERVERINFO\n");
  151. _starpu_top_message_add(_starpu_top_mt,message);
  152. //sending data list
  153. message = (char *) malloc(strlen("DATA\n")+1);
  154. sprintf(message, "%s", "DATA\n");
  155. _starpu_top_message_add(_starpu_top_mt,message);
  156. struct starpu_top_data * cur_data = starpu_top_first_data;
  157. while(cur_data != NULL)
  158. {
  159. _starpu_top_message_add(_starpu_top_mt,message_for_topdata_init(cur_data));
  160. cur_data = cur_data->next;
  161. }
  162. message = (char *) malloc(strlen("/DATA\n")+1);
  163. sprintf(message, "%s", "/DATA\n");
  164. _starpu_top_message_add(_starpu_top_mt,message);
  165. //sending parameter list
  166. message = (char *) malloc(strlen("PARAMS\n")+1);
  167. sprintf(message, "%s", "PARAMS\n");
  168. _starpu_top_message_add(_starpu_top_mt,message);
  169. struct starpu_top_param * cur_param = starpu_top_first_param;
  170. printf("%s:%d sending parameters\n", __FILE__, __LINE__);
  171. while(cur_param != NULL)
  172. {
  173. _starpu_top_message_add(_starpu_top_mt,message_for_topparam_init(cur_param));
  174. cur_param = cur_param->next;
  175. }
  176. printf("%s:%d parameters sended\n", __FILE__, __LINE__);
  177. message = (char *) malloc(strlen("/PARAMS\n")+1);
  178. sprintf(message, "%s", "/PARAMS\n");
  179. _starpu_top_message_add(_starpu_top_mt,message);
  180. //sending DEVICE list
  181. printf("%s:%d sending devices info\n", __FILE__, __LINE__);
  182. starpu_top_send_devices_info();
  183. printf("%s:%d devices_info sended\n", __FILE__, __LINE__);
  184. //copying data and params
  185. copy_data_and_param();
  186. //sending READY message
  187. message = (char *) malloc(strlen("READY\n")+1);
  188. sprintf(message, "%s", "READY\n");
  189. _starpu_top_message_add(_starpu_top_mt,message);
  190. //This threads keeps locked while we don't receive an GO message from UI
  191. printf("%s:%d waiting for GO message\n", __FILE__, __LINE__);
  192. sem_wait(&starpu_top_wait_for_go);
  193. }
  194. void _starpu_top_shutdown(void)
  195. {
  196. {
  197. struct starpu_top_data * cur = starpu_top_first_data, * next;
  198. while (cur) {
  199. next = cur->next;
  200. free(cur);
  201. cur = next;
  202. }
  203. starpu_top_first_data = NULL;
  204. }
  205. {
  206. struct starpu_top_param * cur = starpu_top_first_param, *next;
  207. while (cur) {
  208. next = cur->next;
  209. free(cur);
  210. cur = next;
  211. }
  212. starpu_top_first_param = NULL;
  213. }
  214. }
  215. static
  216. void starpu_top_enqueue_data(struct starpu_top_data * data)
  217. {
  218. if(starpu_top_first_data == NULL)
  219. {
  220. starpu_top_first_data = data;
  221. }
  222. else
  223. {
  224. struct starpu_top_data * cur = starpu_top_first_data;
  225. while(cur->next != NULL)
  226. cur = cur->next;
  227. cur->next = data;
  228. }
  229. }
  230. struct starpu_top_data * starpu_top_add_data_boolean(const char* data_name,
  231. int active)
  232. {
  233. struct starpu_top_data * data = (struct starpu_top_data *) malloc(sizeof(struct starpu_top_data));
  234. data->id = starpu_top_data_cpt++;
  235. data->name = data_name;
  236. data->type = STARPU_TOP_DATA_BOOLEAN;
  237. data->active = active;
  238. data->next = NULL;
  239. starpu_top_enqueue_data(data);
  240. return data;
  241. }
  242. struct starpu_top_data * starpu_top_add_data_integer(const char* data_name,
  243. int minimum_value,
  244. int maximum_value,
  245. int active)
  246. {
  247. struct starpu_top_data * data = (struct starpu_top_data *) malloc(sizeof(struct starpu_top_data));
  248. data->id = starpu_top_data_cpt++;
  249. data->name = data_name;
  250. data->type = STARPU_TOP_DATA_INTEGER;
  251. data->int_min_value = minimum_value;
  252. data->int_max_value = maximum_value;
  253. data->active = active;
  254. data->next = NULL;
  255. starpu_top_enqueue_data(data);
  256. return data;
  257. }
  258. struct starpu_top_data* starpu_top_add_data_float(const char* data_name,
  259. double minimum_value,
  260. double maximum_value,
  261. int active)
  262. {
  263. struct starpu_top_data * data = (struct starpu_top_data *) malloc(sizeof(struct starpu_top_data));
  264. data->id = starpu_top_data_cpt++;
  265. data->name = data_name;
  266. data->type = STARPU_TOP_DATA_FLOAT;
  267. data->double_min_value = minimum_value;
  268. data->double_max_value = maximum_value;
  269. data->active = active;
  270. data->next = NULL;
  271. starpu_top_enqueue_data(data);
  272. return data;
  273. }
  274. char *message_for_topdata_init(struct starpu_top_data* data)
  275. {
  276. char*message = (char *) malloc(256+strlen(data->name));
  277. switch(data->type)
  278. {
  279. case STARPU_TOP_DATA_BOOLEAN:
  280. sprintf(message,
  281. "BOOL;%u;%s;%d\n",
  282. data->id,
  283. data->name,
  284. data->active ? 1 : 0);
  285. break;
  286. case STARPU_TOP_DATA_INTEGER:
  287. sprintf(message,
  288. "INT;%u;%s;%d;%d;%d\n",
  289. data->id,
  290. data->name,
  291. data->int_min_value,
  292. data->int_max_value,
  293. data->active ? 1 : 0);
  294. break;
  295. case STARPU_TOP_DATA_FLOAT:
  296. sprintf(message,
  297. "FLOAT;%u;%s;%f;%f;%d\n",
  298. data->id,
  299. data->name,
  300. data->double_min_value,
  301. data->double_max_value,
  302. data->active ? 1 : 0);
  303. break;
  304. }
  305. return message;
  306. }
  307. char *message_for_topparam_init(struct starpu_top_param* param)
  308. {
  309. char*message = NULL;
  310. int i;
  311. int length=0;
  312. switch(param->type)
  313. {
  314. case STARPU_TOP_PARAM_BOOLEAN:
  315. message = (char *) malloc(256);
  316. sprintf(message,
  317. "BOOL;%u;%s;%d\n",
  318. param->id,
  319. param->name,
  320. (*(int*)(param->value)) ? 1 : 0);
  321. break;
  322. case STARPU_TOP_PARAM_INTEGER:
  323. message = (char *) malloc(256);
  324. sprintf(message,
  325. "INT;%u;%s;%d;%d;%d\n",param->id,
  326. param->name,
  327. param->int_min_value,
  328. param->int_max_value,
  329. *(int*)(param->value));
  330. break;
  331. case STARPU_TOP_PARAM_FLOAT:
  332. message = (char *) malloc(256);
  333. sprintf(message,
  334. "FLOAT;%u;%s;%f;%f;%f\n",
  335. param->id,
  336. param->name,
  337. param->double_min_value,
  338. param->double_max_value,
  339. *(double*)(param->value));
  340. break;
  341. case STARPU_TOP_PARAM_ENUM:
  342. //compute message lenght
  343. for(i = 0; i < param->nb_values; i++)
  344. {
  345. length += strlen(param->enum_values[i])+1;
  346. }
  347. message = (char *) malloc(256+length);
  348. sprintf(message,
  349. "ENUM;%u;%s;",
  350. param->id,
  351. param->name);
  352. //compute the begin of enums elements in message
  353. char* cur = message+strlen(message);
  354. //add each enum element
  355. for(i = 0; i < param->nb_values; i++)
  356. {
  357. strcpy(cur, param->enum_values[i]);
  358. cur+=strlen(cur);
  359. *cur=';';
  360. cur++;
  361. }
  362. sprintf(cur,
  363. "%d\n",
  364. *((int*)(param->value)));
  365. break;
  366. }
  367. return message;
  368. }
  369. static
  370. void starpu_top_enqueue_param(struct starpu_top_param* param)
  371. {
  372. if(starpu_top_first_param == NULL)
  373. {
  374. starpu_top_first_param = param;
  375. }
  376. else
  377. {
  378. struct starpu_top_param * cur = starpu_top_first_param;
  379. while(cur->next != NULL)
  380. cur = cur->next;
  381. cur->next = param;
  382. }
  383. }
  384. struct starpu_top_param* starpu_top_register_parameter_boolean(const char* param_name,
  385. int* parameter_field,
  386. void (*callback)(struct starpu_top_param*))
  387. {
  388. STARPU_ASSERT(!_starpu_top_status_get());
  389. struct starpu_top_param * param = (struct starpu_top_param *) malloc(sizeof(struct starpu_top_param));
  390. param->callback = callback;
  391. param->name = param_name;
  392. param->id = starpu_top_param_cpt++;
  393. param->type = STARPU_TOP_PARAM_BOOLEAN;
  394. param->value = (void*)parameter_field;
  395. param->next = NULL;
  396. starpu_top_enqueue_param(param);
  397. return param;
  398. }
  399. struct starpu_top_param* starpu_top_register_parameter_integer(const char* param_name,
  400. int* parameter_field,
  401. int minimum_value,
  402. int maximum_value,
  403. void (*callback)(struct starpu_top_param*))
  404. {
  405. STARPU_ASSERT(!_starpu_top_status_get());
  406. struct starpu_top_param * param = (struct starpu_top_param *) malloc(sizeof(struct starpu_top_param));
  407. param->callback = callback;
  408. param->name = param_name;
  409. param->id = starpu_top_param_cpt++;
  410. param->type = STARPU_TOP_PARAM_INTEGER;
  411. param->value = (void*)parameter_field;
  412. param->int_min_value = minimum_value;
  413. param->int_max_value = maximum_value;
  414. param->next = NULL;
  415. starpu_top_enqueue_param(param);
  416. return param;
  417. }
  418. struct starpu_top_param* starpu_top_register_parameter_float(const char* param_name,
  419. double* parameter_field,
  420. double minimum_value,
  421. double maximum_value,
  422. void (*callback)(struct starpu_top_param*))
  423. {
  424. STARPU_ASSERT(!_starpu_top_status_get());
  425. struct starpu_top_param * param = (struct starpu_top_param *) malloc(sizeof(struct starpu_top_param));
  426. param->callback = callback;
  427. param->name = param_name;
  428. param->id = starpu_top_param_cpt++;
  429. param->type = STARPU_TOP_PARAM_FLOAT;
  430. param->value = (void*)parameter_field;
  431. param->double_min_value = minimum_value;
  432. param->double_max_value = maximum_value;
  433. param->next = NULL;
  434. starpu_top_enqueue_param(param);
  435. return param;
  436. }
  437. struct starpu_top_param* starpu_top_register_parameter_enum(const char* param_name,
  438. int* parameter_field,
  439. char** values,
  440. int nb_values,
  441. void (*callback)(struct starpu_top_param*))
  442. {
  443. STARPU_ASSERT(!_starpu_top_status_get());
  444. struct starpu_top_param * param = (struct starpu_top_param *) malloc(sizeof(struct starpu_top_param));
  445. param->callback = callback;
  446. param->name = param_name;
  447. param->id = starpu_top_param_cpt++;
  448. param->type = STARPU_TOP_PARAM_ENUM;
  449. param->value = (void*)parameter_field;
  450. param->enum_values = values;
  451. param->nb_values = nb_values;
  452. param->next = NULL;
  453. starpu_top_enqueue_param(param);
  454. return param;
  455. }
  456. /*********************************************
  457. *****************UPDATE FUNC******************
  458. **********************************************/
  459. void starpu_top_update_data_boolean(const struct starpu_top_data* data, int value)
  460. {
  461. if (!_starpu_top_status_get())
  462. return;
  463. if(data->active)
  464. {
  465. char*message = (char *) malloc(256+strlen(data->name));
  466. sprintf(message,
  467. "U;%u;%d;%llu\n",
  468. data->id,
  469. (value?1:0),
  470. current_timestamp());
  471. _starpu_top_message_add(_starpu_top_mt,message);
  472. }
  473. }
  474. void starpu_top_update_data_integer(const struct starpu_top_data* data, int value)
  475. {
  476. if (!_starpu_top_status_get())
  477. return;
  478. if(data->active)
  479. {
  480. char*message = (char *) malloc(256+strlen(data->name));
  481. sprintf(message,
  482. "U;%u;%d;%llu\n",
  483. data->id,
  484. value,
  485. current_timestamp());
  486. _starpu_top_message_add(_starpu_top_mt,message);
  487. }
  488. }
  489. void starpu_top_update_data_float(const struct starpu_top_data* data, double value)
  490. {
  491. if (!_starpu_top_status_get())
  492. return;
  493. if(data->active)
  494. {
  495. char*message = (char *) malloc(256+strlen(data->name));
  496. sprintf(message,
  497. "U;%u;%f;%llu\n",
  498. data->id, value,
  499. current_timestamp());
  500. _starpu_top_message_add(_starpu_top_mt,message);
  501. }
  502. }
  503. void starpu_top_update_parameter(const struct starpu_top_param* param)
  504. {
  505. if (!_starpu_top_status_get())
  506. return;
  507. char*message = (char *) malloc(50);
  508. switch(param->type)
  509. {
  510. case STARPU_TOP_PARAM_BOOLEAN:
  511. case STARPU_TOP_PARAM_INTEGER:
  512. case STARPU_TOP_PARAM_ENUM:
  513. sprintf(message,
  514. "SET;%u;%d;%llu\n",
  515. param->id,
  516. *((int*)param->value),
  517. current_timestamp());
  518. break;
  519. case STARPU_TOP_PARAM_FLOAT:
  520. sprintf(message,
  521. "SET;%u;%f;%llu\n",
  522. param->id,
  523. *((double*)param->value),
  524. current_timestamp());
  525. break;
  526. }
  527. _starpu_top_message_add(_starpu_top_mt,message);
  528. }
  529. /*********************************************
  530. *****************DEBUG FUNC******************
  531. **********************************************/
  532. void starpu_top_debug_log(const char* debug_message)
  533. {
  534. if(starpu_top_debug_on)
  535. {
  536. //length can be up to strlen*2, if message contains only unwanted chars
  537. char * message = (char *) malloc(strlen(debug_message)*2+16);
  538. sprintf(message,"MESSAGE;");
  539. //escape unwanted char : ; and \n
  540. char* cur = message+8;
  541. while(*debug_message!='\0')
  542. {
  543. if(*debug_message=='\n' || *debug_message==';')
  544. {
  545. *cur='\\';
  546. cur++;
  547. }
  548. *cur = *debug_message;
  549. cur++;
  550. debug_message++;
  551. }
  552. *cur='\n';
  553. cur++;
  554. *cur='\0';
  555. _starpu_top_message_add(_starpu_top_mt,message);
  556. }
  557. }
  558. void starpu_top_debug_lock(const char* debug_message)
  559. {
  560. if(starpu_top_debug_on)
  561. {
  562. char * message = (char *) malloc(strlen(debug_message)*2+16);
  563. sprintf(message,"LOCK;");
  564. char* cur = message+5;
  565. while(*debug_message!='\0')
  566. {
  567. if(*debug_message=='\n' || *debug_message==';')
  568. {
  569. *cur='\\';
  570. cur++;
  571. }
  572. *cur = *debug_message;
  573. cur++;
  574. debug_message++;
  575. }
  576. *cur='\n';
  577. *(cur+1)='\0';
  578. _starpu_top_message_add(_starpu_top_mt,message);
  579. //This threads keeps locked while we don't receive an STEP message
  580. STARPU_PTHREAD_MUTEX_LOCK(&starpu_top_wait_for_continue_mutex);
  581. STARPU_PTHREAD_COND_WAIT(&starpu_top_wait_for_continue_cond,
  582. &starpu_top_wait_for_continue_mutex);
  583. STARPU_PTHREAD_MUTEX_UNLOCK(&starpu_top_wait_for_continue_mutex);
  584. }
  585. }
  586. /********************************************
  587. **************TIME FUNCTION****************
  588. *******************************************/
  589. unsigned long long current_timestamp(void)
  590. {
  591. struct timespec now;
  592. _starpu_clock_gettime(&now);
  593. return _starpu_top_timing_timespec_to_ms(&now);
  594. }
  595. unsigned long long _starpu_top_timing_timespec_to_ms(const struct timespec *ts)
  596. {
  597. return (1000.0*ts->tv_sec) + (0.000001*ts->tv_nsec);
  598. }
  599. /********************************************
  600. **************INPUT PROCESSING**************
  601. *******************************************/
  602. static
  603. enum starpu_top_message_type starpu_top_get_message_type(const char* message)
  604. {
  605. if(!strncmp("GO\n", message,3))
  606. return TOP_TYPE_GO;
  607. else if(!strncmp("SET;", message,4))
  608. return TOP_TYPE_SET;
  609. else if(!strncmp("STEP\n", message,9))
  610. return TOP_TYPE_CONTINUE;
  611. else if(!strncmp("ENABLE;", message,7))
  612. return TOP_TYPE_ENABLE;
  613. else if(!strncmp("DISABLE;", message,8))
  614. return TOP_TYPE_DISABLE;
  615. else if(!strncmp("DEBUG;", message,6))
  616. return TOP_TYPE_DEBUG;
  617. else
  618. return TOP_TYPE_UNKNOW;
  619. }
  620. static
  621. void starpu_top_unlock_starpu(void)
  622. {
  623. sem_post(&starpu_top_wait_for_go);
  624. printf("%s:%d starpu started\n", __FILE__, __LINE__);
  625. }
  626. static
  627. void starpu_top_change_data_active(char* message, int active)
  628. {
  629. char* debut = strstr(message, ";")+1;
  630. char* fin = strstr(debut+1, "\n");
  631. *fin = '\0';
  632. int data_id = atoi(debut);
  633. printf("%s:%d data %d %s\n", __FILE__, __LINE__, data_id, active ? "ENABLED" : "DISABLE");
  634. starpu_top_datas[data_id]->active = active;
  635. }
  636. static
  637. void starpu_top_change_parameter_value(const char* message)
  638. {
  639. const char*tmp = strstr(message, ";")+1;
  640. int param_id = atoi(tmp);
  641. struct starpu_top_param* param = starpu_top_params[param_id];
  642. tmp = strstr(tmp+1,";")+1;
  643. int* val_ptr_int;
  644. double* val_ptr_double;
  645. switch(param->type)
  646. {
  647. case STARPU_TOP_PARAM_BOOLEAN:
  648. case STARPU_TOP_PARAM_INTEGER:
  649. val_ptr_int = (int*)param->value;
  650. *val_ptr_int = atoi(tmp);
  651. break;
  652. case STARPU_TOP_PARAM_FLOAT:
  653. val_ptr_double = (double*)param->value;
  654. *val_ptr_double = atof(tmp);
  655. break;
  656. case STARPU_TOP_PARAM_ENUM:
  657. val_ptr_int = (int*)param->value;
  658. *val_ptr_int = atoi(tmp);
  659. break;
  660. }
  661. if(param->callback != NULL)
  662. param->callback(param);
  663. }
  664. static
  665. void starpu_top_change_debug_mode(const char*message)
  666. {
  667. const char* debut = strstr(message, ";")+1;
  668. if(!strncmp("ON",debut, 2))
  669. {
  670. starpu_top_debug_on = 1;
  671. printf("%s:%d debug is now ON\n", __FILE__, __LINE__);
  672. }
  673. else
  674. {
  675. starpu_top_debug_on = 0;
  676. printf("%s:%d debug is now OFF\n", __FILE__, __LINE__);
  677. }
  678. char * m = (char *) malloc(strlen(message)+1);
  679. sprintf(m,"%s",message);
  680. _starpu_top_message_add(_starpu_top_mt,m);
  681. }
  682. /*
  683. * Unlock starpu if it was locked in debug state
  684. */
  685. static
  686. void starpu_top_debug_next_step(void)
  687. {
  688. STARPU_PTHREAD_COND_SIGNAL(&starpu_top_wait_for_continue_cond);
  689. }
  690. void _starpu_top_process_input_message(char *buffer)
  691. {
  692. enum starpu_top_message_type message_type = starpu_top_get_message_type(buffer);
  693. switch(message_type)
  694. {
  695. case TOP_TYPE_GO:
  696. starpu_top_unlock_starpu();
  697. break;
  698. case TOP_TYPE_ENABLE:
  699. starpu_top_change_data_active(buffer, 1);
  700. break;
  701. case TOP_TYPE_DISABLE:
  702. starpu_top_change_data_active(buffer, 0);
  703. break;
  704. case TOP_TYPE_SET:
  705. starpu_top_change_parameter_value(buffer);
  706. break;
  707. case TOP_TYPE_DEBUG:
  708. starpu_top_change_debug_mode(buffer);
  709. break;
  710. case TOP_TYPE_CONTINUE:
  711. starpu_top_debug_next_step();
  712. break;
  713. default:
  714. printf("%s:%d unknow message : '%s'\n", __FILE__, __LINE__, buffer);
  715. }
  716. }