statistics_api.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file statistics/statistics_api.c
  19. * @brief API of the statistics service
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_constants.h"
  25. #include "gnunet_protocols.h"
  26. #include "gnunet_statistics_service.h"
  27. #include "statistics.h"
  28. /**
  29. * How long do we wait until a statistics request for setting
  30. * a value times out? (The update will be lost if the
  31. * service does not react within this timeframe).
  32. */
  33. #define SET_TRANSMIT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
  34. #define LOG(kind,...) GNUNET_log_from (kind, "statistics-api",__VA_ARGS__)
  35. /**
  36. * Types of actions.
  37. */
  38. enum ActionType
  39. {
  40. /**
  41. * Get a value.
  42. */
  43. ACTION_GET,
  44. /**
  45. * Set a value.
  46. */
  47. ACTION_SET,
  48. /**
  49. * Update a value.
  50. */
  51. ACTION_UPDATE,
  52. /**
  53. * Watch a value.
  54. */
  55. ACTION_WATCH
  56. };
  57. /**
  58. * Entry kept for each value we are watching.
  59. */
  60. struct GNUNET_STATISTICS_WatchEntry
  61. {
  62. /**
  63. * What subsystem is this action about? (never NULL)
  64. */
  65. char *subsystem;
  66. /**
  67. * What value is this action about? (never NULL)
  68. */
  69. char *name;
  70. /**
  71. * Function to call
  72. */
  73. GNUNET_STATISTICS_Iterator proc;
  74. /**
  75. * Closure for proc
  76. */
  77. void *proc_cls;
  78. };
  79. /**
  80. * Linked list of things we still need to do.
  81. */
  82. struct GNUNET_STATISTICS_GetHandle
  83. {
  84. /**
  85. * This is a doubly linked list.
  86. */
  87. struct GNUNET_STATISTICS_GetHandle *next;
  88. /**
  89. * This is a doubly linked list.
  90. */
  91. struct GNUNET_STATISTICS_GetHandle *prev;
  92. /**
  93. * Main statistics handle.
  94. */
  95. struct GNUNET_STATISTICS_Handle *sh;
  96. /**
  97. * What subsystem is this action about? (can be NULL)
  98. */
  99. char *subsystem;
  100. /**
  101. * What value is this action about? (can be NULL)
  102. */
  103. char *name;
  104. /**
  105. * Continuation to call once action is complete.
  106. */
  107. GNUNET_STATISTICS_Callback cont;
  108. /**
  109. * Function to call (for GET actions only).
  110. */
  111. GNUNET_STATISTICS_Iterator proc;
  112. /**
  113. * Closure for proc and cont.
  114. */
  115. void *cls;
  116. /**
  117. * Timeout for this action.
  118. */
  119. struct GNUNET_TIME_Absolute timeout;
  120. /**
  121. * Task run on timeout.
  122. */
  123. GNUNET_SCHEDULER_TaskIdentifier timeout_task;
  124. /**
  125. * Associated value.
  126. */
  127. uint64_t value;
  128. /**
  129. * Flag for SET/UPDATE actions.
  130. */
  131. int make_persistent;
  132. /**
  133. * Has the current iteration been aborted; for GET actions.
  134. */
  135. int aborted;
  136. /**
  137. * Is this a GET, SET, UPDATE or WATCH?
  138. */
  139. enum ActionType type;
  140. /**
  141. * Size of the message that we will be transmitting.
  142. */
  143. uint16_t msize;
  144. };
  145. /**
  146. * Handle for the service.
  147. */
  148. struct GNUNET_STATISTICS_Handle
  149. {
  150. /**
  151. * Name of our subsystem.
  152. */
  153. char *subsystem;
  154. /**
  155. * Configuration to use.
  156. */
  157. const struct GNUNET_CONFIGURATION_Handle *cfg;
  158. /**
  159. * Socket (if available).
  160. */
  161. struct GNUNET_CLIENT_Connection *client;
  162. /**
  163. * Currently pending transmission request.
  164. */
  165. struct GNUNET_CLIENT_TransmitHandle *th;
  166. /**
  167. * Head of the linked list of pending actions (first action
  168. * to be performed).
  169. */
  170. struct GNUNET_STATISTICS_GetHandle *action_head;
  171. /**
  172. * Tail of the linked list of actions (for fast append).
  173. */
  174. struct GNUNET_STATISTICS_GetHandle *action_tail;
  175. /**
  176. * Action we are currently busy with (action request has been
  177. * transmitted, we're now receiving the response from the
  178. * service).
  179. */
  180. struct GNUNET_STATISTICS_GetHandle *current;
  181. /**
  182. * Array of watch entries.
  183. */
  184. struct GNUNET_STATISTICS_WatchEntry **watches;
  185. /**
  186. * Task doing exponential back-off trying to reconnect.
  187. */
  188. GNUNET_SCHEDULER_TaskIdentifier backoff_task;
  189. /**
  190. * Time for next connect retry.
  191. */
  192. struct GNUNET_TIME_Relative backoff;
  193. /**
  194. * Maximum heap size observed so far (if available).
  195. */
  196. uint64_t peak_heap_size;
  197. /**
  198. * Maximum resident set side observed so far (if available).
  199. */
  200. uint64_t peak_rss;
  201. /**
  202. * Size of the 'watches' array.
  203. */
  204. unsigned int watches_size;
  205. /**
  206. * Should this handle auto-destruct once all actions have
  207. * been processed?
  208. */
  209. int do_destroy;
  210. /**
  211. * Are we currently receiving from the service?
  212. */
  213. int receiving;
  214. };
  215. /**
  216. * Obtain statistics about this process's memory consumption and
  217. * report those as well (if they changed).
  218. */
  219. static void
  220. update_memory_statistics (struct GNUNET_STATISTICS_Handle *h)
  221. {
  222. #if ENABLE_HEAP_STATISTICS
  223. uint64_t current_heap_size = 0;
  224. uint64_t current_rss = 0;
  225. if (GNUNET_NO != h->do_destroy)
  226. return;
  227. #if HAVE_MALLINFO
  228. {
  229. struct mallinfo mi;
  230. mi = mallinfo();
  231. current_heap_size = mi.uordblks + mi.fordblks;
  232. }
  233. #endif
  234. #if HAVE_GETRUSAGE
  235. {
  236. struct rusage ru;
  237. if (0 == getrusage (RUSAGE_SELF, &ru))
  238. {
  239. current_rss = 1024LL * ru.ru_maxrss;
  240. }
  241. }
  242. #endif
  243. if (current_heap_size > h->peak_heap_size)
  244. {
  245. h->peak_heap_size = current_heap_size;
  246. GNUNET_STATISTICS_set (h, "# peak heap size", current_heap_size, GNUNET_NO);
  247. }
  248. if (current_rss > h->peak_rss)
  249. {
  250. h->peak_rss = current_rss;
  251. GNUNET_STATISTICS_set (h, "# peak resident set size", current_rss, GNUNET_NO);
  252. }
  253. #endif
  254. }
  255. /**
  256. * Schedule the next action to be performed.
  257. *
  258. * @param h statistics handle to reconnect
  259. */
  260. static void
  261. schedule_action (struct GNUNET_STATISTICS_Handle *h);
  262. /**
  263. * Transmit request to service that we want to watch
  264. * the development of a particular value.
  265. *
  266. * @param h statistics handle
  267. * @param watch watch entry of the value to watch
  268. */
  269. static void
  270. schedule_watch_request (struct GNUNET_STATISTICS_Handle *h,
  271. struct GNUNET_STATISTICS_WatchEntry *watch)
  272. {
  273. struct GNUNET_STATISTICS_GetHandle *ai;
  274. size_t slen;
  275. size_t nlen;
  276. size_t nsize;
  277. slen = strlen (watch->subsystem) + 1;
  278. nlen = strlen (watch->name) + 1;
  279. nsize = sizeof (struct GNUNET_MessageHeader) + slen + nlen;
  280. if (nsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
  281. {
  282. GNUNET_break (0);
  283. return;
  284. }
  285. ai = GNUNET_new (struct GNUNET_STATISTICS_GetHandle);
  286. ai->sh = h;
  287. ai->subsystem = GNUNET_strdup (watch->subsystem);
  288. ai->name = GNUNET_strdup (watch->name);
  289. ai->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
  290. ai->msize = nsize;
  291. ai->type = ACTION_WATCH;
  292. ai->proc = watch->proc;
  293. ai->cls = watch->proc_cls;
  294. GNUNET_CONTAINER_DLL_insert_tail (h->action_head, h->action_tail,
  295. ai);
  296. schedule_action (h);
  297. }
  298. /**
  299. * Free memory associated with the given action item.
  300. *
  301. * @param gh action item to free
  302. */
  303. static void
  304. free_action_item (struct GNUNET_STATISTICS_GetHandle *gh)
  305. {
  306. if (GNUNET_SCHEDULER_NO_TASK != gh->timeout_task)
  307. {
  308. GNUNET_SCHEDULER_cancel (gh->timeout_task);
  309. gh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
  310. }
  311. GNUNET_free_non_null (gh->subsystem);
  312. GNUNET_free_non_null (gh->name);
  313. GNUNET_free (gh);
  314. }
  315. /**
  316. * Disconnect from the statistics service.
  317. *
  318. * @param h statistics handle to disconnect from
  319. */
  320. static void
  321. do_disconnect (struct GNUNET_STATISTICS_Handle *h)
  322. {
  323. struct GNUNET_STATISTICS_GetHandle *c;
  324. if (NULL != h->th)
  325. {
  326. GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
  327. h->th = NULL;
  328. }
  329. h->receiving = GNUNET_NO;
  330. if (NULL != (c = h->current))
  331. {
  332. h->current = NULL;
  333. if ( (NULL != c->cont) &&
  334. (GNUNET_YES != c->aborted) )
  335. {
  336. c->cont (c->cls, GNUNET_SYSERR);
  337. c->cont = NULL;
  338. }
  339. free_action_item (c);
  340. }
  341. if (NULL != h->client)
  342. {
  343. GNUNET_CLIENT_disconnect (h->client);
  344. h->client = NULL;
  345. }
  346. }
  347. /**
  348. * Try to (re)connect to the statistics service.
  349. *
  350. * @param h statistics handle to reconnect
  351. * @return #GNUNET_YES on success, #GNUNET_NO on failure.
  352. */
  353. static int
  354. try_connect (struct GNUNET_STATISTICS_Handle *h)
  355. {
  356. struct GNUNET_STATISTICS_GetHandle *gh;
  357. struct GNUNET_STATISTICS_GetHandle *gn;
  358. unsigned int i;
  359. if (GNUNET_SCHEDULER_NO_TASK != h->backoff_task)
  360. return GNUNET_NO;
  361. if (NULL != h->client)
  362. return GNUNET_YES;
  363. h->client = GNUNET_CLIENT_connect ("statistics", h->cfg);
  364. if (NULL != h->client)
  365. {
  366. gn = h->action_head;
  367. while (NULL != (gh = gn))
  368. {
  369. gn = gh->next;
  370. if (gh->type == ACTION_WATCH)
  371. {
  372. GNUNET_CONTAINER_DLL_remove (h->action_head,
  373. h->action_tail,
  374. gh);
  375. free_action_item (gh);
  376. }
  377. }
  378. for (i = 0; i < h->watches_size; i++)
  379. {
  380. if (NULL != h->watches[i])
  381. schedule_watch_request (h, h->watches[i]);
  382. }
  383. return GNUNET_YES;
  384. }
  385. LOG (GNUNET_ERROR_TYPE_DEBUG,
  386. "Failed to connect to statistics service!\n");
  387. return GNUNET_NO;
  388. }
  389. /**
  390. * We've waited long enough, reconnect now.
  391. *
  392. * @param cls the `struct GNUNET_STATISTICS_Handle` to reconnect
  393. * @param tc scheduler context (unused)
  394. */
  395. static void
  396. reconnect_task (void *cls,
  397. const struct GNUNET_SCHEDULER_TaskContext *tc)
  398. {
  399. struct GNUNET_STATISTICS_Handle *h = cls;
  400. h->backoff_task = GNUNET_SCHEDULER_NO_TASK;
  401. schedule_action (h);
  402. }
  403. /**
  404. * Task used by 'reconnect_later' to shutdown the handle
  405. *
  406. * @param cls the statistics handle
  407. * @param tc scheduler context
  408. */
  409. static void
  410. do_destroy (void *cls,
  411. const struct GNUNET_SCHEDULER_TaskContext *tc)
  412. {
  413. struct GNUNET_STATISTICS_Handle *h = cls;
  414. GNUNET_STATISTICS_destroy (h, GNUNET_NO);
  415. }
  416. /**
  417. * Reconnect at a later time, respecting back-off.
  418. *
  419. * @param h statistics handle
  420. */
  421. static void
  422. reconnect_later (struct GNUNET_STATISTICS_Handle *h)
  423. {
  424. int loss;
  425. struct GNUNET_STATISTICS_GetHandle *gh;
  426. GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->backoff_task);
  427. if (GNUNET_YES == h->do_destroy)
  428. {
  429. /* So we are shutting down and the service is not reachable.
  430. * Chances are that it's down for good and we are not going to connect to
  431. * it anymore.
  432. * Give up and don't sync the rest of the data.
  433. */
  434. loss = GNUNET_NO;
  435. for (gh = h->action_head; NULL != gh; gh = gh->next)
  436. if ( (gh->make_persistent) && (ACTION_SET == gh->type) )
  437. loss = GNUNET_YES;
  438. if (GNUNET_YES == loss)
  439. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  440. _("Could not save some persistent statistics\n"));
  441. h->do_destroy = GNUNET_NO;
  442. GNUNET_SCHEDULER_add_continuation (&do_destroy, h,
  443. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  444. return;
  445. }
  446. h->backoff_task =
  447. GNUNET_SCHEDULER_add_delayed (h->backoff, &reconnect_task, h);
  448. h->backoff = GNUNET_TIME_STD_BACKOFF (h->backoff);
  449. }
  450. /**
  451. * Process a #GNUNET_MESSAGE_TYPE_STATISTICS_VALUE message.
  452. *
  453. * @param h statistics handle
  454. * @param msg message received from the service, never NULL
  455. * @return #GNUNET_OK if the message was well-formed
  456. */
  457. static int
  458. process_statistics_value_message (struct GNUNET_STATISTICS_Handle *h,
  459. const struct GNUNET_MessageHeader *msg)
  460. {
  461. char *service;
  462. char *name;
  463. const struct GNUNET_STATISTICS_ReplyMessage *smsg;
  464. uint16_t size;
  465. if (h->current->aborted)
  466. {
  467. LOG (GNUNET_ERROR_TYPE_DEBUG,
  468. "Iteration was aborted, ignoring VALUE\n");
  469. return GNUNET_OK; /* don't bother */
  470. }
  471. size = ntohs (msg->size);
  472. if (size < sizeof (struct GNUNET_STATISTICS_ReplyMessage))
  473. {
  474. GNUNET_break (0);
  475. return GNUNET_SYSERR;
  476. }
  477. smsg = (const struct GNUNET_STATISTICS_ReplyMessage *) msg;
  478. size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
  479. if (size !=
  480. GNUNET_STRINGS_buffer_tokenize ((const char *) &smsg[1], size, 2,
  481. &service, &name))
  482. {
  483. GNUNET_break (0);
  484. return GNUNET_SYSERR;
  485. }
  486. LOG (GNUNET_ERROR_TYPE_DEBUG,
  487. "Received valid statistic on `%s:%s': %llu\n",
  488. service, name,
  489. GNUNET_ntohll (smsg->value));
  490. if (GNUNET_OK !=
  491. h->current->proc (h->current->cls, service, name,
  492. GNUNET_ntohll (smsg->value),
  493. 0 !=
  494. (ntohl (smsg->uid) & GNUNET_STATISTICS_PERSIST_BIT)))
  495. {
  496. LOG (GNUNET_ERROR_TYPE_DEBUG,
  497. "Processing of remaining statistics aborted by client.\n");
  498. h->current->aborted = GNUNET_YES;
  499. }
  500. LOG (GNUNET_ERROR_TYPE_DEBUG,
  501. "VALUE processed successfully\n");
  502. return GNUNET_OK;
  503. }
  504. /**
  505. * We have received a watch value from the service. Process it.
  506. *
  507. * @param h statistics handle
  508. * @param msg the watch value message
  509. * @return #GNUNET_OK if the message was well-formed, #GNUNET_SYSERR if not,
  510. * #GNUNET_NO if this watch has been cancelled
  511. */
  512. static int
  513. process_watch_value (struct GNUNET_STATISTICS_Handle *h,
  514. const struct GNUNET_MessageHeader *msg)
  515. {
  516. const struct GNUNET_STATISTICS_WatchValueMessage *wvm;
  517. struct GNUNET_STATISTICS_WatchEntry *w;
  518. uint32_t wid;
  519. if (sizeof (struct GNUNET_STATISTICS_WatchValueMessage) != ntohs (msg->size))
  520. {
  521. GNUNET_break (0);
  522. return GNUNET_SYSERR;
  523. }
  524. wvm = (const struct GNUNET_STATISTICS_WatchValueMessage *) msg;
  525. GNUNET_break (0 == ntohl (wvm->reserved));
  526. wid = ntohl (wvm->wid);
  527. if (wid >= h->watches_size)
  528. {
  529. GNUNET_break (0);
  530. return GNUNET_SYSERR;
  531. }
  532. w = h->watches[wid];
  533. if (NULL == w)
  534. return GNUNET_NO;
  535. (void) w->proc (w->proc_cls, w->subsystem, w->name,
  536. GNUNET_ntohll (wvm->value),
  537. 0 != (ntohl (wvm->flags) & GNUNET_STATISTICS_PERSIST_BIT));
  538. return GNUNET_OK;
  539. }
  540. /**
  541. * Task used to destroy the statistics handle.
  542. *
  543. * @param cls the `struct GNUNET_STATISTICS_Handle`
  544. * @param tc the scheduler context
  545. */
  546. static void
  547. destroy_task (void *cls,
  548. const struct GNUNET_SCHEDULER_TaskContext *tc)
  549. {
  550. struct GNUNET_STATISTICS_Handle *h = cls;
  551. GNUNET_STATISTICS_destroy (h, GNUNET_NO);
  552. }
  553. /**
  554. * Function called with messages from stats service.
  555. *
  556. * @param cls closure
  557. * @param msg message received, NULL on timeout or fatal error
  558. */
  559. static void
  560. receive_stats (void *cls,
  561. const struct GNUNET_MessageHeader *msg)
  562. {
  563. struct GNUNET_STATISTICS_Handle *h = cls;
  564. struct GNUNET_STATISTICS_GetHandle *c;
  565. int ret;
  566. if (NULL == msg)
  567. {
  568. LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
  569. "Error receiving statistics from service, is the service running?\n");
  570. do_disconnect (h);
  571. reconnect_later (h);
  572. return;
  573. }
  574. switch (ntohs (msg->type))
  575. {
  576. case GNUNET_MESSAGE_TYPE_TEST:
  577. if (GNUNET_SYSERR != h->do_destroy)
  578. {
  579. /* not in shutdown, why do we get 'TEST'? */
  580. GNUNET_break (0);
  581. do_disconnect (h);
  582. reconnect_later (h);
  583. return;
  584. }
  585. h->do_destroy = GNUNET_NO;
  586. GNUNET_SCHEDULER_add_continuation (&destroy_task, h,
  587. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  588. break;
  589. case GNUNET_MESSAGE_TYPE_STATISTICS_END:
  590. LOG (GNUNET_ERROR_TYPE_DEBUG,
  591. "Received end of statistics marker\n");
  592. if (NULL == (c = h->current))
  593. {
  594. GNUNET_break (0);
  595. do_disconnect (h);
  596. reconnect_later (h);
  597. return;
  598. }
  599. h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
  600. if (h->watches_size > 0)
  601. {
  602. GNUNET_CLIENT_receive (h->client, &receive_stats, h,
  603. GNUNET_TIME_UNIT_FOREVER_REL);
  604. }
  605. else
  606. {
  607. h->receiving = GNUNET_NO;
  608. }
  609. h->current = NULL;
  610. schedule_action (h);
  611. if (NULL != c->cont)
  612. {
  613. c->cont (c->cls, GNUNET_OK);
  614. c->cont = NULL;
  615. }
  616. free_action_item (c);
  617. return;
  618. case GNUNET_MESSAGE_TYPE_STATISTICS_VALUE:
  619. if (GNUNET_OK != process_statistics_value_message (h, msg))
  620. {
  621. do_disconnect (h);
  622. reconnect_later (h);
  623. return;
  624. }
  625. /* finally, look for more! */
  626. LOG (GNUNET_ERROR_TYPE_DEBUG,
  627. "Processing VALUE done, now reading more\n");
  628. GNUNET_CLIENT_receive (h->client, &receive_stats, h,
  629. GNUNET_TIME_absolute_get_remaining (h->
  630. current->timeout));
  631. h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
  632. return;
  633. case GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE:
  634. if (GNUNET_OK !=
  635. (ret = process_watch_value (h, msg)))
  636. {
  637. do_disconnect (h);
  638. if (GNUNET_NO == ret)
  639. h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
  640. reconnect_later (h);
  641. return;
  642. }
  643. h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
  644. GNUNET_assert (h->watches_size > 0);
  645. GNUNET_CLIENT_receive (h->client, &receive_stats, h,
  646. GNUNET_TIME_UNIT_FOREVER_REL);
  647. return;
  648. default:
  649. GNUNET_break (0);
  650. do_disconnect (h);
  651. reconnect_later (h);
  652. return;
  653. }
  654. }
  655. /**
  656. * Transmit a GET request (and if successful, start to receive
  657. * the response).
  658. *
  659. * @param handle statistics handle
  660. * @param size how many bytes can we write to @a buf
  661. * @param buf where to write requests to the service
  662. * @return number of bytes written to @a buf
  663. */
  664. static size_t
  665. transmit_get (struct GNUNET_STATISTICS_Handle *handle,
  666. size_t size,
  667. void *buf)
  668. {
  669. struct GNUNET_STATISTICS_GetHandle *c;
  670. struct GNUNET_MessageHeader *hdr;
  671. size_t slen1;
  672. size_t slen2;
  673. uint16_t msize;
  674. GNUNET_assert (NULL != (c = handle->current));
  675. if (NULL == buf)
  676. {
  677. /* timeout / error */
  678. LOG (GNUNET_ERROR_TYPE_DEBUG,
  679. "Transmission of request for statistics failed!\n");
  680. do_disconnect (handle);
  681. reconnect_later (handle);
  682. return 0;
  683. }
  684. slen1 = strlen (c->subsystem) + 1;
  685. slen2 = strlen (c->name) + 1;
  686. msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
  687. GNUNET_assert (msize <= size);
  688. hdr = (struct GNUNET_MessageHeader *) buf;
  689. hdr->size = htons (msize);
  690. hdr->type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_GET);
  691. GNUNET_assert (slen1 + slen2 ==
  692. GNUNET_STRINGS_buffer_fill ((char *) &hdr[1], slen1 + slen2, 2,
  693. c->subsystem,
  694. c->name));
  695. if (GNUNET_YES != handle->receiving)
  696. {
  697. LOG (GNUNET_ERROR_TYPE_DEBUG,
  698. "Transmission of GET done, now reading response\n");
  699. handle->receiving = GNUNET_YES;
  700. GNUNET_CLIENT_receive (handle->client, &receive_stats, handle,
  701. GNUNET_TIME_absolute_get_remaining (c->timeout));
  702. }
  703. return msize;
  704. }
  705. /**
  706. * Transmit a WATCH request (and if successful, start to receive
  707. * the response).
  708. *
  709. * @param handle statistics handle
  710. * @param size how many bytes can we write to @a buf
  711. * @param buf where to write requests to the service
  712. * @return number of bytes written to @a buf
  713. */
  714. static size_t
  715. transmit_watch (struct GNUNET_STATISTICS_Handle *handle,
  716. size_t size,
  717. void *buf)
  718. {
  719. struct GNUNET_MessageHeader *hdr;
  720. size_t slen1;
  721. size_t slen2;
  722. uint16_t msize;
  723. if (NULL == buf)
  724. {
  725. /* timeout / error */
  726. LOG (GNUNET_ERROR_TYPE_DEBUG,
  727. "Transmission of request for statistics failed!\n");
  728. do_disconnect (handle);
  729. reconnect_later (handle);
  730. return 0;
  731. }
  732. LOG (GNUNET_ERROR_TYPE_DEBUG,
  733. "Transmitting watch request for `%s'\n",
  734. handle->current->name);
  735. slen1 = strlen (handle->current->subsystem) + 1;
  736. slen2 = strlen (handle->current->name) + 1;
  737. msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
  738. GNUNET_assert (msize <= size);
  739. hdr = (struct GNUNET_MessageHeader *) buf;
  740. hdr->size = htons (msize);
  741. hdr->type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH);
  742. GNUNET_assert (slen1 + slen2 ==
  743. GNUNET_STRINGS_buffer_fill ((char *) &hdr[1], slen1 + slen2, 2,
  744. handle->current->subsystem,
  745. handle->current->name));
  746. if (GNUNET_YES != handle->receiving)
  747. {
  748. handle->receiving = GNUNET_YES;
  749. GNUNET_CLIENT_receive (handle->client, &receive_stats, handle,
  750. GNUNET_TIME_UNIT_FOREVER_REL);
  751. }
  752. GNUNET_assert (NULL == handle->current->cont);
  753. free_action_item (handle->current);
  754. handle->current = NULL;
  755. return msize;
  756. }
  757. /**
  758. * Transmit a SET/UPDATE request.
  759. *
  760. * @param handle statistics handle
  761. * @param size how many bytes can we write to @a buf
  762. * @param buf where to write requests to the service
  763. * @return number of bytes written to @a buf
  764. */
  765. static size_t
  766. transmit_set (struct GNUNET_STATISTICS_Handle *handle,
  767. size_t size,
  768. void *buf)
  769. {
  770. struct GNUNET_STATISTICS_SetMessage *r;
  771. size_t slen;
  772. size_t nlen;
  773. size_t nsize;
  774. if (NULL == buf)
  775. {
  776. do_disconnect (handle);
  777. reconnect_later (handle);
  778. return 0;
  779. }
  780. slen = strlen (handle->current->subsystem) + 1;
  781. nlen = strlen (handle->current->name) + 1;
  782. nsize = sizeof (struct GNUNET_STATISTICS_SetMessage) + slen + nlen;
  783. if (size < nsize)
  784. {
  785. GNUNET_break (0);
  786. do_disconnect (handle);
  787. reconnect_later (handle);
  788. return 0;
  789. }
  790. r = buf;
  791. r->header.size = htons (nsize);
  792. r->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
  793. r->flags = 0;
  794. r->value = GNUNET_htonll (handle->current->value);
  795. if (handle->current->make_persistent)
  796. r->flags |= htonl (GNUNET_STATISTICS_SETFLAG_PERSISTENT);
  797. if (handle->current->type == ACTION_UPDATE)
  798. r->flags |= htonl (GNUNET_STATISTICS_SETFLAG_RELATIVE);
  799. GNUNET_assert (slen + nlen ==
  800. GNUNET_STRINGS_buffer_fill ((char *) &r[1], slen + nlen, 2,
  801. handle->current->subsystem,
  802. handle->current->name));
  803. GNUNET_assert (NULL == handle->current->cont);
  804. free_action_item (handle->current);
  805. handle->current = NULL;
  806. update_memory_statistics (handle);
  807. return nsize;
  808. }
  809. /**
  810. * Function called when we are ready to transmit a request to the service.
  811. *
  812. * @param cls the `struct GNUNET_STATISTICS_Handle`
  813. * @param size how many bytes can we write to @a buf
  814. * @param buf where to write requests to the service
  815. * @return number of bytes written to @a buf
  816. */
  817. static size_t
  818. transmit_action (void *cls, size_t size, void *buf)
  819. {
  820. struct GNUNET_STATISTICS_Handle *h = cls;
  821. size_t ret;
  822. h->th = NULL;
  823. ret = 0;
  824. if (NULL != h->current)
  825. switch (h->current->type)
  826. {
  827. case ACTION_GET:
  828. ret = transmit_get (h, size, buf);
  829. break;
  830. case ACTION_SET:
  831. case ACTION_UPDATE:
  832. ret = transmit_set (h, size, buf);
  833. break;
  834. case ACTION_WATCH:
  835. ret = transmit_watch (h, size, buf);
  836. break;
  837. default:
  838. GNUNET_assert (0);
  839. break;
  840. }
  841. schedule_action (h);
  842. return ret;
  843. }
  844. /**
  845. * Get handle for the statistics service.
  846. *
  847. * @param subsystem name of subsystem using the service
  848. * @param cfg services configuration in use
  849. * @return handle to use
  850. */
  851. struct GNUNET_STATISTICS_Handle *
  852. GNUNET_STATISTICS_create (const char *subsystem,
  853. const struct GNUNET_CONFIGURATION_Handle *cfg)
  854. {
  855. struct GNUNET_STATISTICS_Handle *ret;
  856. if (GNUNET_YES ==
  857. GNUNET_CONFIGURATION_get_value_yesno (cfg, "statistics", "DISABLE"))
  858. return NULL;
  859. GNUNET_assert (NULL != subsystem);
  860. GNUNET_assert (NULL != cfg);
  861. ret = GNUNET_new (struct GNUNET_STATISTICS_Handle);
  862. ret->cfg = cfg;
  863. ret->subsystem = GNUNET_strdup (subsystem);
  864. ret->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
  865. return ret;
  866. }
  867. /**
  868. * Destroy a handle (free all state associated with
  869. * it).
  870. *
  871. * @param h statistics handle to destroy
  872. * @param sync_first set to #GNUNET_YES if pending SET requests should
  873. * be completed
  874. */
  875. void
  876. GNUNET_STATISTICS_destroy (struct GNUNET_STATISTICS_Handle *h,
  877. int sync_first)
  878. {
  879. struct GNUNET_STATISTICS_GetHandle *pos;
  880. struct GNUNET_STATISTICS_GetHandle *next;
  881. struct GNUNET_TIME_Relative timeout;
  882. int i;
  883. if (NULL == h)
  884. return;
  885. GNUNET_assert (GNUNET_NO == h->do_destroy); // Don't call twice.
  886. if (GNUNET_SCHEDULER_NO_TASK != h->backoff_task)
  887. {
  888. GNUNET_SCHEDULER_cancel (h->backoff_task);
  889. h->backoff_task = GNUNET_SCHEDULER_NO_TASK;
  890. }
  891. if (sync_first)
  892. {
  893. if (NULL != h->current)
  894. {
  895. if (ACTION_GET == h->current->type)
  896. {
  897. if (NULL != h->th)
  898. {
  899. GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
  900. h->th = NULL;
  901. }
  902. free_action_item (h->current);
  903. h->current = NULL;
  904. }
  905. }
  906. next = h->action_head;
  907. while (NULL != (pos = next))
  908. {
  909. next = pos->next;
  910. if (ACTION_GET == pos->type)
  911. {
  912. GNUNET_CONTAINER_DLL_remove (h->action_head,
  913. h->action_tail,
  914. pos);
  915. free_action_item (pos);
  916. }
  917. }
  918. if ( (NULL == h->current) &&
  919. (NULL != (h->current = h->action_head)) )
  920. GNUNET_CONTAINER_DLL_remove (h->action_head,
  921. h->action_tail,
  922. h->current);
  923. h->do_destroy = GNUNET_YES;
  924. if ((NULL != h->current) && (NULL == h->th) &&
  925. (NULL != h->client))
  926. {
  927. timeout = GNUNET_TIME_absolute_get_remaining (h->current->timeout);
  928. h->th =
  929. GNUNET_CLIENT_notify_transmit_ready (h->client, h->current->msize,
  930. timeout, GNUNET_YES,
  931. &transmit_action, h);
  932. GNUNET_assert (NULL != h->th);
  933. }
  934. if (NULL != h->th)
  935. return; /* do not finish destruction just yet */
  936. }
  937. while (NULL != (pos = h->action_head))
  938. {
  939. GNUNET_CONTAINER_DLL_remove (h->action_head,
  940. h->action_tail,
  941. pos);
  942. free_action_item (pos);
  943. }
  944. do_disconnect (h);
  945. for (i = 0; i < h->watches_size; i++)
  946. {
  947. if (NULL == h->watches[i])
  948. continue;
  949. GNUNET_free (h->watches[i]->subsystem);
  950. GNUNET_free (h->watches[i]->name);
  951. GNUNET_free (h->watches[i]);
  952. }
  953. GNUNET_array_grow (h->watches, h->watches_size, 0);
  954. GNUNET_free (h->subsystem);
  955. GNUNET_free (h);
  956. }
  957. /**
  958. * Function called to transmit TEST message to service to
  959. * confirm that the service has received all of our 'SET'
  960. * messages (during statistics disconnect/shutdown).
  961. *
  962. * @param cls the `struct GNUNET_STATISTICS_Handle`
  963. * @param size how many bytes can we write to @a buf
  964. * @param buf where to write requests to the service
  965. * @return number of bytes written to @a buf
  966. */
  967. static size_t
  968. transmit_test_on_shutdown (void *cls,
  969. size_t size,
  970. void *buf)
  971. {
  972. struct GNUNET_STATISTICS_Handle *h = cls;
  973. struct GNUNET_MessageHeader hdr;
  974. h->th = NULL;
  975. if (NULL == buf)
  976. {
  977. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  978. _("Failed to receive acknowledgement from statistics service, some statistics might have been lost!\n"));
  979. h->do_destroy = GNUNET_NO;
  980. GNUNET_SCHEDULER_add_continuation (&destroy_task, h,
  981. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  982. return 0;
  983. }
  984. hdr.type = htons (GNUNET_MESSAGE_TYPE_TEST);
  985. hdr.size = htons (sizeof (struct GNUNET_MessageHeader));
  986. memcpy (buf, &hdr, sizeof (hdr));
  987. if (GNUNET_YES != h->receiving)
  988. {
  989. h->receiving = GNUNET_YES;
  990. GNUNET_CLIENT_receive (h->client, &receive_stats, h,
  991. GNUNET_TIME_UNIT_FOREVER_REL);
  992. }
  993. return sizeof (struct GNUNET_MessageHeader);
  994. }
  995. /**
  996. * Schedule the next action to be performed.
  997. *
  998. * @param h statistics handle
  999. */
  1000. static void
  1001. schedule_action (struct GNUNET_STATISTICS_Handle *h)
  1002. {
  1003. struct GNUNET_TIME_Relative timeout;
  1004. if ( (NULL != h->th) ||
  1005. (GNUNET_SCHEDULER_NO_TASK != h->backoff_task) )
  1006. return; /* action already pending */
  1007. if (GNUNET_YES != try_connect (h))
  1008. {
  1009. reconnect_later (h);
  1010. return;
  1011. }
  1012. if (NULL != h->current)
  1013. return; /* action already pending */
  1014. /* schedule next action */
  1015. h->current = h->action_head;
  1016. if (NULL == h->current)
  1017. {
  1018. if (GNUNET_YES == h->do_destroy)
  1019. {
  1020. h->do_destroy = GNUNET_SYSERR; /* in 'TEST' mode */
  1021. h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
  1022. sizeof (struct GNUNET_MessageHeader),
  1023. SET_TRANSMIT_TIMEOUT,
  1024. GNUNET_NO,
  1025. &transmit_test_on_shutdown, h);
  1026. }
  1027. return;
  1028. }
  1029. GNUNET_CONTAINER_DLL_remove (h->action_head, h->action_tail, h->current);
  1030. timeout = GNUNET_TIME_absolute_get_remaining (h->current->timeout);
  1031. if (NULL ==
  1032. (h->th =
  1033. GNUNET_CLIENT_notify_transmit_ready (h->client, h->current->msize,
  1034. timeout, GNUNET_YES,
  1035. &transmit_action, h)))
  1036. {
  1037. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1038. "Failed to transmit request to statistics service.\n");
  1039. do_disconnect (h);
  1040. reconnect_later (h);
  1041. }
  1042. }
  1043. /**
  1044. * We have run into a timeout on a #GNUNET_STATISTICS_get() operation,
  1045. * call the continuation.
  1046. *
  1047. * @param cls the `struct GNUNET_STATISTICS_GetHandle`
  1048. * @param tc scheduler context
  1049. */
  1050. static void
  1051. run_get_timeout (void *cls,
  1052. const struct GNUNET_SCHEDULER_TaskContext *tc)
  1053. {
  1054. struct GNUNET_STATISTICS_GetHandle *gh = cls;
  1055. GNUNET_STATISTICS_Callback cont = gh->cont;
  1056. void *cont_cls = gh->cls;
  1057. gh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
  1058. GNUNET_STATISTICS_get_cancel (gh);
  1059. cont (cont_cls, GNUNET_SYSERR);
  1060. }
  1061. /**
  1062. * Get statistic from the peer.
  1063. *
  1064. * @param handle identification of the statistics service
  1065. * @param subsystem limit to the specified subsystem, NULL for our subsystem
  1066. * @param name name of the statistic value, NULL for all values
  1067. * @param timeout after how long should we give up (and call
  1068. * cont with an error code)?
  1069. * @param cont continuation to call when done (can be NULL)
  1070. * This callback CANNOT destroy the statistics handle in the same call.
  1071. * @param proc function to call on each value
  1072. * @param cls closure for @a cont and @a proc
  1073. * @return NULL on error
  1074. */
  1075. struct GNUNET_STATISTICS_GetHandle *
  1076. GNUNET_STATISTICS_get (struct GNUNET_STATISTICS_Handle *handle,
  1077. const char *subsystem, const char *name,
  1078. struct GNUNET_TIME_Relative timeout,
  1079. GNUNET_STATISTICS_Callback cont,
  1080. GNUNET_STATISTICS_Iterator proc, void *cls)
  1081. {
  1082. size_t slen1;
  1083. size_t slen2;
  1084. struct GNUNET_STATISTICS_GetHandle *ai;
  1085. if (NULL == handle)
  1086. return NULL;
  1087. GNUNET_assert (NULL != proc);
  1088. GNUNET_assert (GNUNET_NO == handle->do_destroy);
  1089. if (NULL == subsystem)
  1090. subsystem = "";
  1091. if (NULL == name)
  1092. name = "";
  1093. slen1 = strlen (subsystem) + 1;
  1094. slen2 = strlen (name) + 1;
  1095. GNUNET_assert (slen1 + slen2 + sizeof (struct GNUNET_MessageHeader) <
  1096. GNUNET_SERVER_MAX_MESSAGE_SIZE);
  1097. ai = GNUNET_new (struct GNUNET_STATISTICS_GetHandle);
  1098. ai->sh = handle;
  1099. ai->subsystem = GNUNET_strdup (subsystem);
  1100. ai->name = GNUNET_strdup (name);
  1101. ai->cont = cont;
  1102. ai->proc = proc;
  1103. ai->cls = cls;
  1104. ai->timeout = GNUNET_TIME_relative_to_absolute (timeout);
  1105. ai->type = ACTION_GET;
  1106. ai->msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
  1107. ai->timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
  1108. &run_get_timeout,
  1109. ai);
  1110. GNUNET_CONTAINER_DLL_insert_tail (handle->action_head, handle->action_tail,
  1111. ai);
  1112. schedule_action (handle);
  1113. return ai;
  1114. }
  1115. /**
  1116. * Cancel a 'get' request. Must be called before the 'cont'
  1117. * function is called.
  1118. *
  1119. * @param gh handle of the request to cancel
  1120. */
  1121. void
  1122. GNUNET_STATISTICS_get_cancel (struct GNUNET_STATISTICS_GetHandle *gh)
  1123. {
  1124. if (NULL == gh)
  1125. return;
  1126. if (GNUNET_SCHEDULER_NO_TASK != gh->timeout_task)
  1127. {
  1128. GNUNET_SCHEDULER_cancel (gh->timeout_task);
  1129. gh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
  1130. }
  1131. gh->cont = NULL;
  1132. if (gh->sh->current == gh)
  1133. {
  1134. gh->aborted = GNUNET_YES;
  1135. }
  1136. else
  1137. {
  1138. GNUNET_CONTAINER_DLL_remove (gh->sh->action_head, gh->sh->action_tail, gh);
  1139. GNUNET_free (gh->name);
  1140. GNUNET_free (gh->subsystem);
  1141. GNUNET_free (gh);
  1142. }
  1143. }
  1144. /**
  1145. * Watch statistics from the peer (be notified whenever they change).
  1146. *
  1147. * @param handle identification of the statistics service
  1148. * @param subsystem limit to the specified subsystem, never NULL
  1149. * @param name name of the statistic value, never NULL
  1150. * @param proc function to call on each value
  1151. * @param proc_cls closure for @a proc
  1152. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  1153. */
  1154. int
  1155. GNUNET_STATISTICS_watch (struct GNUNET_STATISTICS_Handle *handle,
  1156. const char *subsystem, const char *name,
  1157. GNUNET_STATISTICS_Iterator proc, void *proc_cls)
  1158. {
  1159. struct GNUNET_STATISTICS_WatchEntry *w;
  1160. if (NULL == handle)
  1161. return GNUNET_SYSERR;
  1162. w = GNUNET_new (struct GNUNET_STATISTICS_WatchEntry);
  1163. w->subsystem = GNUNET_strdup (subsystem);
  1164. w->name = GNUNET_strdup (name);
  1165. w->proc = proc;
  1166. w->proc_cls = proc_cls;
  1167. GNUNET_array_append (handle->watches, handle->watches_size, w);
  1168. schedule_watch_request (handle, w);
  1169. return GNUNET_OK;
  1170. }
  1171. /**
  1172. * Stop watching statistics from the peer.
  1173. *
  1174. * @param handle identification of the statistics service
  1175. * @param subsystem limit to the specified subsystem, never NULL
  1176. * @param name name of the statistic value, never NULL
  1177. * @param proc function to call on each value
  1178. * @param proc_cls closure for @a proc
  1179. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error (no such watch)
  1180. */
  1181. int
  1182. GNUNET_STATISTICS_watch_cancel (struct GNUNET_STATISTICS_Handle *handle,
  1183. const char *subsystem,
  1184. const char *name,
  1185. GNUNET_STATISTICS_Iterator proc,
  1186. void *proc_cls)
  1187. {
  1188. struct GNUNET_STATISTICS_WatchEntry *w;
  1189. unsigned int i;
  1190. if (NULL == handle)
  1191. return GNUNET_SYSERR;
  1192. for (i=0;i<handle->watches_size;i++)
  1193. {
  1194. w = handle->watches[i];
  1195. if (NULL == w)
  1196. continue;
  1197. if ( (w->proc == proc) &&
  1198. (w->proc_cls == proc_cls) &&
  1199. (0 == strcmp (w->name, name)) &&
  1200. (0 == strcmp (w->subsystem, subsystem)) )
  1201. {
  1202. GNUNET_free (w->name);
  1203. GNUNET_free (w->subsystem);
  1204. GNUNET_free (w);
  1205. handle->watches[i] = NULL;
  1206. return GNUNET_OK;
  1207. }
  1208. }
  1209. return GNUNET_SYSERR;
  1210. }
  1211. /**
  1212. * Queue a request to change a statistic.
  1213. *
  1214. * @param h statistics handle
  1215. * @param name name of the value
  1216. * @param make_persistent should the value be kept across restarts?
  1217. * @param value new value or change
  1218. * @param type type of the action (#ACTION_SET or #ACTION_UPDATE)
  1219. */
  1220. static void
  1221. add_setter_action (struct GNUNET_STATISTICS_Handle *h,
  1222. const char *name,
  1223. int make_persistent,
  1224. uint64_t value,
  1225. enum ActionType type)
  1226. {
  1227. struct GNUNET_STATISTICS_GetHandle *ai;
  1228. size_t slen;
  1229. size_t nlen;
  1230. size_t nsize;
  1231. int64_t delta;
  1232. slen = strlen (h->subsystem) + 1;
  1233. nlen = strlen (name) + 1;
  1234. nsize = sizeof (struct GNUNET_STATISTICS_SetMessage) + slen + nlen;
  1235. if (nsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
  1236. {
  1237. GNUNET_break (0);
  1238. return;
  1239. }
  1240. for (ai = h->action_head; NULL != ai; ai = ai->next)
  1241. {
  1242. if (! ( (0 == strcmp (ai->subsystem, h->subsystem)) &&
  1243. (0 == strcmp (ai->name, name)) &&
  1244. ( (ACTION_UPDATE == ai->type) ||
  1245. (ACTION_SET == ai->type) ) ) )
  1246. continue;
  1247. if (ACTION_SET == ai->type)
  1248. {
  1249. if (ACTION_UPDATE == type)
  1250. {
  1251. delta = (int64_t) value;
  1252. if (delta > 0)
  1253. {
  1254. /* update old set by new delta */
  1255. ai->value += delta;
  1256. }
  1257. else
  1258. {
  1259. /* update old set by new delta, but never go negative */
  1260. if (ai->value < -delta)
  1261. ai->value = 0;
  1262. else
  1263. ai->value += delta;
  1264. }
  1265. }
  1266. else
  1267. {
  1268. /* new set overrides old set */
  1269. ai->value = value;
  1270. }
  1271. }
  1272. else
  1273. {
  1274. if (ACTION_UPDATE == type)
  1275. {
  1276. /* make delta cummulative */
  1277. delta = (int64_t) value;
  1278. ai->value += delta;
  1279. }
  1280. else
  1281. {
  1282. /* drop old 'update', use new 'set' instead */
  1283. ai->value = value;
  1284. ai->type = type;
  1285. }
  1286. }
  1287. ai->timeout = GNUNET_TIME_relative_to_absolute (SET_TRANSMIT_TIMEOUT);
  1288. ai->make_persistent = make_persistent;
  1289. return;
  1290. }
  1291. /* no existing entry matches, create a fresh one */
  1292. ai = GNUNET_new (struct GNUNET_STATISTICS_GetHandle);
  1293. ai->sh = h;
  1294. ai->subsystem = GNUNET_strdup (h->subsystem);
  1295. ai->name = GNUNET_strdup (name);
  1296. ai->timeout = GNUNET_TIME_relative_to_absolute (SET_TRANSMIT_TIMEOUT);
  1297. ai->make_persistent = make_persistent;
  1298. ai->msize = nsize;
  1299. ai->value = value;
  1300. ai->type = type;
  1301. GNUNET_CONTAINER_DLL_insert_tail (h->action_head, h->action_tail,
  1302. ai);
  1303. schedule_action (h);
  1304. }
  1305. /**
  1306. * Set statistic value for the peer. Will always use our
  1307. * subsystem (the argument used when "handle" was created).
  1308. *
  1309. * @param handle identification of the statistics service
  1310. * @param name name of the statistic value
  1311. * @param value new value to set
  1312. * @param make_persistent should the value be kept across restarts?
  1313. */
  1314. void
  1315. GNUNET_STATISTICS_set (struct GNUNET_STATISTICS_Handle *handle,
  1316. const char *name,
  1317. uint64_t value,
  1318. int make_persistent)
  1319. {
  1320. if (NULL == handle)
  1321. return;
  1322. GNUNET_assert (GNUNET_NO == handle->do_destroy);
  1323. add_setter_action (handle, name, make_persistent, value, ACTION_SET);
  1324. }
  1325. /**
  1326. * Set statistic value for the peer. Will always use our
  1327. * subsystem (the argument used when "handle" was created).
  1328. *
  1329. * @param handle identification of the statistics service
  1330. * @param name name of the statistic value
  1331. * @param delta change in value (added to existing value)
  1332. * @param make_persistent should the value be kept across restarts?
  1333. */
  1334. void
  1335. GNUNET_STATISTICS_update (struct GNUNET_STATISTICS_Handle *handle,
  1336. const char *name,
  1337. int64_t delta,
  1338. int make_persistent)
  1339. {
  1340. if (NULL == handle)
  1341. return;
  1342. if (0 == delta)
  1343. return;
  1344. GNUNET_assert (GNUNET_NO == handle->do_destroy);
  1345. add_setter_action (handle,
  1346. name,
  1347. make_persistent,
  1348. (uint64_t) delta,
  1349. ACTION_UPDATE);
  1350. }
  1351. /* end of statistics_api.c */