gnunet-service-statistics.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009, 2010, 2012, 2014, 2016 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your 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. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file statistics/gnunet-service-statistics.c
  18. * @brief program that tracks statistics
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_bio_lib.h"
  23. #include "gnunet_container_lib.h"
  24. #include "gnunet_disk_lib.h"
  25. #include "gnunet_getopt_lib.h"
  26. #include "gnunet_protocols.h"
  27. #include "gnunet_service_lib.h"
  28. #include "gnunet_statistics_service.h"
  29. #include "gnunet_strings_lib.h"
  30. #include "gnunet_time_lib.h"
  31. #include "statistics.h"
  32. /**
  33. * Watch entry.
  34. */
  35. struct WatchEntry
  36. {
  37. /**
  38. * Watch entries are kept in a linked list.
  39. */
  40. struct WatchEntry *next;
  41. /**
  42. * Watch entries are kept in a linked list.
  43. */
  44. struct WatchEntry *prev;
  45. /**
  46. * For which client is this watch entry?
  47. */
  48. struct ClientEntry *ce;
  49. /**
  50. * Last value we communicated to the client for this watch entry.
  51. */
  52. uint64_t last_value;
  53. /**
  54. * Unique watch number for this client and this watched value.
  55. */
  56. uint32_t wid;
  57. /**
  58. * Is last_value valid
  59. * #GNUNET_NO : last_value is n/a, #GNUNET_YES: last_value is valid
  60. */
  61. int last_value_set;
  62. };
  63. /**
  64. * We keep the statistics organized by subsystem for faster
  65. * lookup during SET operations.
  66. */
  67. struct SubsystemEntry;
  68. /**
  69. * Entry in the statistics list.
  70. */
  71. struct StatsEntry
  72. {
  73. /**
  74. * This is a linked list.
  75. */
  76. struct StatsEntry *next;
  77. /**
  78. * This is a linked list.
  79. */
  80. struct StatsEntry *prev;
  81. /**
  82. * Subsystem this entry belongs to.
  83. */
  84. struct SubsystemEntry *subsystem;
  85. /**
  86. * Name for the value stored by this entry, allocated at the end of
  87. * this struct.
  88. */
  89. const char *name;
  90. /**
  91. * Watch context for changes to this value, or NULL for none.
  92. */
  93. struct WatchEntry *we_head;
  94. /**
  95. * Watch context for changes to this value, or NULL for none.
  96. */
  97. struct WatchEntry *we_tail;
  98. /**
  99. * Our value.
  100. */
  101. uint64_t value;
  102. /**
  103. * Unique ID.
  104. */
  105. uint32_t uid;
  106. /**
  107. * Is this value persistent?
  108. */
  109. int persistent;
  110. /**
  111. * Is this value set?
  112. * #GNUNET_NO: value is n/a, #GNUNET_YES: value is valid
  113. */
  114. int set;
  115. };
  116. /**
  117. * We keep the statistics organized by subsystem for faster
  118. * lookup during SET operations.
  119. */
  120. struct SubsystemEntry
  121. {
  122. /**
  123. * Subsystems are kept in a DLL.
  124. */
  125. struct SubsystemEntry *next;
  126. /**
  127. * Subsystems are kept in a DLL.
  128. */
  129. struct SubsystemEntry *prev;
  130. /**
  131. * Head of list of values kept for this subsystem.
  132. */
  133. struct StatsEntry *stat_head;
  134. /**
  135. * Tail of list of values kept for this subsystem.
  136. */
  137. struct StatsEntry *stat_tail;
  138. /**
  139. * Name of the subsystem this entry is for, allocated at
  140. * the end of this struct, do not free().
  141. */
  142. const char *service;
  143. };
  144. /**
  145. * Client entry.
  146. */
  147. struct ClientEntry
  148. {
  149. /**
  150. * Corresponding server handle.
  151. */
  152. struct GNUNET_SERVICE_Client *client;
  153. /**
  154. * Corresponding message queue.
  155. */
  156. struct GNUNET_MQ_Handle *mq;
  157. /**
  158. * Which subsystem is this client writing to (SET/UPDATE)?
  159. */
  160. struct SubsystemEntry *subsystem;
  161. /**
  162. * Maximum watch ID used by this client so far.
  163. */
  164. uint32_t max_wid;
  165. };
  166. /**
  167. * Our configuration.
  168. */
  169. static const struct GNUNET_CONFIGURATION_Handle *cfg;
  170. /**
  171. * Head of linked list of subsystems with active statistics.
  172. */
  173. static struct SubsystemEntry *sub_head;
  174. /**
  175. * Tail of linked list of subsystems with active statistics.
  176. */
  177. static struct SubsystemEntry *sub_tail;
  178. /**
  179. * Number of connected clients.
  180. */
  181. static unsigned int client_count;
  182. /**
  183. * Our notification context.
  184. */
  185. static struct GNUNET_NotificationContext *nc;
  186. /**
  187. * Counter used to generate unique values.
  188. */
  189. static uint32_t uidgen;
  190. /**
  191. * Set to #GNUNET_YES if we are shutting down as soon as possible.
  192. */
  193. static int in_shutdown;
  194. /**
  195. * Write persistent statistics to disk.
  196. */
  197. static void
  198. save ()
  199. {
  200. struct SubsystemEntry *se;
  201. struct StatsEntry *pos;
  202. char *fn;
  203. struct GNUNET_BIO_WriteHandle *wh;
  204. uint16_t size;
  205. unsigned long long total;
  206. size_t nlen;
  207. size_t slen;
  208. struct GNUNET_STATISTICS_SetMessage *msg;
  209. if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg,
  210. "STATISTICS",
  211. "DATABASE",
  212. &fn))
  213. {
  214. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
  215. "STATISTICS",
  216. "DATABASE");
  217. return;
  218. }
  219. (void) GNUNET_DISK_directory_create_for_file (fn);
  220. wh = GNUNET_BIO_write_open_file (fn);
  221. total = 0;
  222. while (NULL != (se = sub_head))
  223. {
  224. GNUNET_CONTAINER_DLL_remove (sub_head, sub_tail, se);
  225. slen = strlen (se->service) + 1;
  226. while (NULL != (pos = se->stat_head))
  227. {
  228. GNUNET_CONTAINER_DLL_remove (se->stat_head, se->stat_tail, pos);
  229. if ((pos->persistent) && (NULL != wh))
  230. {
  231. nlen = strlen (pos->name) + 1;
  232. size = sizeof(struct GNUNET_STATISTICS_SetMessage) + nlen + slen;
  233. GNUNET_assert (size < UINT16_MAX);
  234. msg = GNUNET_malloc (size);
  235. msg->header.size = htons ((uint16_t) size);
  236. msg->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
  237. GNUNET_assert (nlen + slen ==
  238. GNUNET_STRINGS_buffer_fill ((char *) &msg[1],
  239. nlen + slen,
  240. 2,
  241. se->service,
  242. pos->name));
  243. msg->flags =
  244. htonl (pos->persistent ? GNUNET_STATISTICS_SETFLAG_PERSISTENT : 0);
  245. msg->value = GNUNET_htonll (pos->value);
  246. if (GNUNET_OK != GNUNET_BIO_write (wh, "statistics-save-msg", msg, size))
  247. {
  248. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
  249. if (GNUNET_OK != GNUNET_BIO_write_close (wh, NULL))
  250. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", fn);
  251. wh = NULL;
  252. }
  253. else
  254. {
  255. total += size;
  256. }
  257. GNUNET_free (msg);
  258. }
  259. GNUNET_free (pos);
  260. }
  261. GNUNET_free (se);
  262. }
  263. if (NULL != wh)
  264. {
  265. if (GNUNET_OK != GNUNET_BIO_write_close (wh, NULL))
  266. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", fn);
  267. if (0 == total)
  268. GNUNET_break (0 == unlink (fn));
  269. else
  270. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  271. _ ("Wrote %llu bytes of statistics to `%s'\n"),
  272. total,
  273. fn);
  274. }
  275. GNUNET_free_non_null (fn);
  276. }
  277. /**
  278. * Transmit the given stats value.
  279. *
  280. * @param client receiver of the value
  281. * @param e value to transmit
  282. */
  283. static void
  284. transmit (struct ClientEntry *ce, const struct StatsEntry *e)
  285. {
  286. struct GNUNET_MQ_Envelope *env;
  287. struct GNUNET_STATISTICS_ReplyMessage *m;
  288. size_t size;
  289. size = strlen (e->subsystem->service) + 1 + strlen (e->name) + 1;
  290. GNUNET_assert (size < GNUNET_MAX_MESSAGE_SIZE);
  291. env = GNUNET_MQ_msg_extra (m, size, GNUNET_MESSAGE_TYPE_STATISTICS_VALUE);
  292. m->uid = htonl (e->uid);
  293. if (e->persistent)
  294. m->uid |= htonl (GNUNET_STATISTICS_PERSIST_BIT);
  295. m->value = GNUNET_htonll (e->value);
  296. GNUNET_assert (size == GNUNET_STRINGS_buffer_fill ((char *) &m[1],
  297. size,
  298. 2,
  299. e->subsystem->service,
  300. e->name));
  301. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  302. "Transmitting value for `%s:%s' (%d): %llu\n",
  303. e->subsystem->service,
  304. e->name,
  305. e->persistent,
  306. (unsigned long long) e->value);
  307. GNUNET_MQ_send (ce->mq, env);
  308. }
  309. /**
  310. * Callback called when a client connects to the service.
  311. *
  312. * @param cls closure for the service
  313. * @param c the new client that connected to the service
  314. * @param mq the message queue used to send messages to the client
  315. * @return @a c
  316. */
  317. static void *
  318. client_connect_cb (void *cls,
  319. struct GNUNET_SERVICE_Client *c,
  320. struct GNUNET_MQ_Handle *mq)
  321. {
  322. struct ClientEntry *ce;
  323. ce = GNUNET_new (struct ClientEntry);
  324. ce->client = c;
  325. ce->mq = mq;
  326. client_count++;
  327. GNUNET_notification_context_add (nc, mq);
  328. return ce;
  329. }
  330. /**
  331. * Check integrity of GET-message.
  332. *
  333. * @param cls identification of the client
  334. * @param message the actual message
  335. * @return #GNUNET_OK if @a message is well-formed
  336. */
  337. static int
  338. check_get (void *cls, const struct GNUNET_MessageHeader *message)
  339. {
  340. const char *service;
  341. const char *name;
  342. size_t size;
  343. size = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
  344. if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
  345. size,
  346. 2,
  347. &service,
  348. &name))
  349. {
  350. GNUNET_break (0);
  351. return GNUNET_SYSERR;
  352. }
  353. return GNUNET_OK;
  354. }
  355. /**
  356. * Handle GET-message.
  357. *
  358. * @param cls identification of the client
  359. * @param message the actual message
  360. */
  361. static void
  362. handle_get (void *cls, const struct GNUNET_MessageHeader *message)
  363. {
  364. struct ClientEntry *ce = cls;
  365. struct GNUNET_MQ_Envelope *env;
  366. struct GNUNET_MessageHeader *end;
  367. const char *service;
  368. const char *name;
  369. size_t slen;
  370. size_t nlen;
  371. struct SubsystemEntry *se;
  372. struct StatsEntry *pos;
  373. size_t size;
  374. size = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
  375. GNUNET_assert (size ==
  376. GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
  377. size,
  378. 2,
  379. &service,
  380. &name));
  381. slen = strlen (service);
  382. nlen = strlen (name);
  383. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  384. "Received request for statistics on `%s:%s'\n",
  385. slen ? service : "*",
  386. nlen ? name : "*");
  387. for (se = sub_head; NULL != se; se = se->next)
  388. {
  389. if (! ((0 == slen) || (0 == strcmp (service, se->service))))
  390. continue;
  391. for (pos = se->stat_head; NULL != pos; pos = pos->next)
  392. {
  393. if (! ((0 == nlen) || (0 == strcmp (name, pos->name))))
  394. continue;
  395. transmit (ce, pos);
  396. }
  397. }
  398. env = GNUNET_MQ_msg (end, GNUNET_MESSAGE_TYPE_STATISTICS_END);
  399. GNUNET_MQ_send (ce->mq, env);
  400. GNUNET_SERVICE_client_continue (ce->client);
  401. }
  402. /**
  403. * Notify all clients listening about a change to a value.
  404. *
  405. * @param se value that changed
  406. */
  407. static void
  408. notify_change (struct StatsEntry *se)
  409. {
  410. struct GNUNET_MQ_Envelope *env;
  411. struct GNUNET_STATISTICS_WatchValueMessage *wvm;
  412. struct WatchEntry *pos;
  413. for (pos = se->we_head; NULL != pos; pos = pos->next)
  414. {
  415. if (GNUNET_YES == pos->last_value_set)
  416. {
  417. if (pos->last_value == se->value)
  418. continue;
  419. }
  420. else
  421. {
  422. pos->last_value_set = GNUNET_YES;
  423. }
  424. env = GNUNET_MQ_msg (wvm, GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE);
  425. wvm->flags =
  426. htonl (se->persistent ? GNUNET_STATISTICS_SETFLAG_PERSISTENT : 0);
  427. wvm->wid = htonl (pos->wid);
  428. wvm->reserved = htonl (0);
  429. wvm->value = GNUNET_htonll (se->value);
  430. GNUNET_MQ_send (pos->ce->mq, env);
  431. pos->last_value = se->value;
  432. }
  433. }
  434. /**
  435. * Find the subsystem entry of the given name for the specified client.
  436. *
  437. * @param ce client looking for the subsystem, may contain a hint
  438. * to find the entry faster, can be NULL
  439. * @param service name of the subsystem to look for
  440. * @return subsystem entry, never NULL (subsystem entry is created if necessary)
  441. */
  442. static struct SubsystemEntry *
  443. find_subsystem_entry (struct ClientEntry *ce, const char *service)
  444. {
  445. size_t slen;
  446. struct SubsystemEntry *se;
  447. if (NULL != ce)
  448. se = ce->subsystem;
  449. else
  450. se = NULL;
  451. if ((NULL == se) || (0 != strcmp (service, se->service)))
  452. {
  453. for (se = sub_head; NULL != se; se = se->next)
  454. if (0 == strcmp (service, se->service))
  455. break;
  456. if (NULL != ce)
  457. ce->subsystem = se;
  458. }
  459. if (NULL != se)
  460. return se;
  461. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  462. "Allocating new subsystem entry `%s'\n",
  463. service);
  464. slen = strlen (service) + 1;
  465. se = GNUNET_malloc (sizeof(struct SubsystemEntry) + slen);
  466. GNUNET_memcpy (&se[1], service, slen);
  467. se->service = (const char *) &se[1];
  468. GNUNET_CONTAINER_DLL_insert (sub_head, sub_tail, se);
  469. if (NULL != ce)
  470. ce->subsystem = se;
  471. return se;
  472. }
  473. /**
  474. * Find the statistics entry of the given subsystem.
  475. *
  476. * @param subsystem subsystem to look in
  477. * @param name name of the entry to look for
  478. * @return statistis entry, or NULL if not found
  479. */
  480. static struct StatsEntry *
  481. find_stat_entry (struct SubsystemEntry *se, const char *name)
  482. {
  483. struct StatsEntry *pos;
  484. for (pos = se->stat_head; NULL != pos; pos = pos->next)
  485. if (0 == strcmp (name, pos->name))
  486. return pos;
  487. return NULL;
  488. }
  489. /**
  490. * Check format of SET-message.
  491. *
  492. * @param cls the `struct ClientEntry`
  493. * @param message the actual message
  494. * @return #GNUNET_OK if message is well-formed
  495. */
  496. static int
  497. check_set (void *cls, const struct GNUNET_STATISTICS_SetMessage *msg)
  498. {
  499. const char *service;
  500. const char *name;
  501. size_t msize;
  502. msize = ntohs (msg->header.size) - sizeof(*msg);
  503. if (msize != GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1],
  504. msize,
  505. 2,
  506. &service,
  507. &name))
  508. {
  509. GNUNET_break (0);
  510. return GNUNET_SYSERR;
  511. }
  512. return GNUNET_OK;
  513. }
  514. /**
  515. * Handle SET-message.
  516. *
  517. * @param cls the `struct ClientEntry`
  518. * @param message the actual message
  519. */
  520. static void
  521. handle_set (void *cls, const struct GNUNET_STATISTICS_SetMessage *msg)
  522. {
  523. struct ClientEntry *ce = cls;
  524. const char *service;
  525. const char *name;
  526. size_t nlen;
  527. uint16_t msize;
  528. uint16_t size;
  529. struct SubsystemEntry *se;
  530. struct StatsEntry *pos;
  531. uint32_t flags;
  532. uint64_t value;
  533. int64_t delta;
  534. int changed;
  535. int initial_set;
  536. msize = ntohs (msg->header.size);
  537. size = msize - sizeof(struct GNUNET_STATISTICS_SetMessage);
  538. GNUNET_assert (size == GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1],
  539. size,
  540. 2,
  541. &service,
  542. &name));
  543. se = find_subsystem_entry (ce, service);
  544. flags = ntohl (msg->flags);
  545. value = GNUNET_ntohll (msg->value);
  546. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  547. "Received request to update statistic on `%s:%s' (%u) to/by %llu\n",
  548. service,
  549. name,
  550. (unsigned int) flags,
  551. (unsigned long long) value);
  552. pos = find_stat_entry (se, name);
  553. if (NULL != pos)
  554. {
  555. initial_set = 0;
  556. if (0 == (flags & GNUNET_STATISTICS_SETFLAG_RELATIVE))
  557. {
  558. changed = (pos->value != value);
  559. pos->value = value;
  560. }
  561. else
  562. {
  563. delta = (int64_t) value;
  564. if ((delta < 0) && (pos->value < -delta))
  565. {
  566. changed = (0 != pos->value);
  567. pos->value = 0;
  568. }
  569. else
  570. {
  571. changed = (0 != delta);
  572. GNUNET_break ((delta <= 0) || (pos->value + delta > pos->value));
  573. pos->value += delta;
  574. }
  575. }
  576. if (GNUNET_NO == pos->set)
  577. {
  578. pos->set = GNUNET_YES;
  579. initial_set = 1;
  580. }
  581. pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
  582. if (pos != se->stat_head)
  583. {
  584. /* move to front for faster setting next time! */
  585. GNUNET_CONTAINER_DLL_remove (se->stat_head, se->stat_tail, pos);
  586. GNUNET_CONTAINER_DLL_insert (se->stat_head, se->stat_tail, pos);
  587. }
  588. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  589. "Statistic `%s:%s' updated to value %llu (%d).\n",
  590. service,
  591. name,
  592. (unsigned long long) pos->value,
  593. pos->persistent);
  594. if ((changed) || (1 == initial_set))
  595. notify_change (pos);
  596. GNUNET_SERVICE_client_continue (ce->client);
  597. return;
  598. }
  599. /* not found, create a new entry */
  600. nlen = strlen (name) + 1;
  601. pos = GNUNET_malloc (sizeof(struct StatsEntry) + nlen);
  602. GNUNET_memcpy (&pos[1], name, nlen);
  603. pos->name = (const char *) &pos[1];
  604. pos->subsystem = se;
  605. if ((0 == (flags & GNUNET_STATISTICS_SETFLAG_RELATIVE)) ||
  606. (0 < (int64_t) GNUNET_ntohll (msg->value)))
  607. {
  608. pos->value = GNUNET_ntohll (msg->value);
  609. pos->set = GNUNET_YES;
  610. }
  611. else
  612. {
  613. pos->set = GNUNET_NO;
  614. }
  615. pos->uid = uidgen++;
  616. pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
  617. GNUNET_CONTAINER_DLL_insert (se->stat_head, se->stat_tail, pos);
  618. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  619. "New statistic on `%s:%s' with value %llu created.\n",
  620. service,
  621. name,
  622. (unsigned long long) pos->value);
  623. if (NULL != ce)
  624. GNUNET_SERVICE_client_continue (ce->client);
  625. }
  626. /**
  627. * Check integrity of WATCH-message.
  628. *
  629. * @param cls the `struct ClientEntry *`
  630. * @param message the actual message
  631. * @return #GNUNET_OK if message is well-formed
  632. */
  633. static int
  634. check_watch (void *cls, const struct GNUNET_MessageHeader *message)
  635. {
  636. size_t size;
  637. const char *service;
  638. const char *name;
  639. size = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
  640. if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
  641. size,
  642. 2,
  643. &service,
  644. &name))
  645. {
  646. GNUNET_break (0);
  647. return GNUNET_SYSERR;
  648. }
  649. return GNUNET_OK;
  650. }
  651. /**
  652. * Handle WATCH-message.
  653. *
  654. * @param cls the `struct ClientEntry *`
  655. * @param message the actual message
  656. */
  657. static void
  658. handle_watch (void *cls, const struct GNUNET_MessageHeader *message)
  659. {
  660. struct ClientEntry *ce = cls;
  661. const char *service;
  662. const char *name;
  663. uint16_t msize;
  664. uint16_t size;
  665. struct SubsystemEntry *se;
  666. struct StatsEntry *pos;
  667. struct WatchEntry *we;
  668. size_t nlen;
  669. if (NULL == nc)
  670. {
  671. GNUNET_SERVICE_client_drop (ce->client);
  672. return;
  673. }
  674. GNUNET_SERVICE_client_mark_monitor (ce->client);
  675. msize = ntohs (message->size);
  676. size = msize - sizeof(struct GNUNET_MessageHeader);
  677. GNUNET_assert (size ==
  678. GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
  679. size,
  680. 2,
  681. &service,
  682. &name));
  683. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  684. "Received request to watch statistic on `%s:%s'\n",
  685. service,
  686. name);
  687. se = find_subsystem_entry (ce, service);
  688. pos = find_stat_entry (se, name);
  689. if (NULL == pos)
  690. {
  691. nlen = strlen (name) + 1;
  692. pos = GNUNET_malloc (sizeof(struct StatsEntry) + nlen);
  693. GNUNET_memcpy (&pos[1], name, nlen);
  694. pos->name = (const char *) &pos[1];
  695. pos->subsystem = se;
  696. GNUNET_CONTAINER_DLL_insert (se->stat_head, se->stat_tail, pos);
  697. pos->uid = uidgen++;
  698. pos->set = GNUNET_NO;
  699. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  700. "New statistic on `%s:%s' with value %llu created.\n",
  701. service,
  702. name,
  703. (unsigned long long) pos->value);
  704. }
  705. we = GNUNET_new (struct WatchEntry);
  706. we->ce = ce;
  707. we->last_value_set = GNUNET_NO;
  708. we->wid = ce->max_wid++;
  709. GNUNET_CONTAINER_DLL_insert (pos->we_head, pos->we_tail, we);
  710. if (0 != pos->value)
  711. notify_change (pos);
  712. GNUNET_SERVICE_client_continue (ce->client);
  713. }
  714. /**
  715. * Handle DISCONNECT-message. Sync to disk and send
  716. * back a #GNUNET_MESSAGE_TYPE_STATISTICS_DISCONNECT_CONFIRM
  717. * message.
  718. *
  719. * @param cls the `struct ClientEntry *`
  720. * @param message the actual message
  721. */
  722. static void
  723. handle_disconnect (void *cls, const struct GNUNET_MessageHeader *message)
  724. {
  725. struct ClientEntry *ce = cls;
  726. struct GNUNET_MQ_Envelope *env;
  727. struct GNUNET_MessageHeader *msg;
  728. env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_STATISTICS_DISCONNECT_CONFIRM);
  729. GNUNET_MQ_send (ce->mq, env);
  730. GNUNET_SERVICE_client_continue (ce->client);
  731. }
  732. /**
  733. * Actually perform the shutdown.
  734. */
  735. static void
  736. do_shutdown ()
  737. {
  738. struct WatchEntry *we;
  739. struct StatsEntry *pos;
  740. struct SubsystemEntry *se;
  741. if (NULL == nc)
  742. return;
  743. save ();
  744. GNUNET_notification_context_destroy (nc);
  745. nc = NULL;
  746. GNUNET_assert (0 == client_count);
  747. while (NULL != (se = sub_head))
  748. {
  749. GNUNET_CONTAINER_DLL_remove (sub_head, sub_tail, se);
  750. while (NULL != (pos = se->stat_head))
  751. {
  752. GNUNET_CONTAINER_DLL_remove (se->stat_head, se->stat_tail, pos);
  753. while (NULL != (we = pos->we_head))
  754. {
  755. GNUNET_break (0);
  756. GNUNET_CONTAINER_DLL_remove (pos->we_head, pos->we_tail, we);
  757. GNUNET_free (we);
  758. }
  759. GNUNET_free (pos);
  760. }
  761. GNUNET_free (se);
  762. }
  763. }
  764. /**
  765. * Task run during shutdown.
  766. *
  767. * @param cls unused
  768. */
  769. static void
  770. shutdown_task (void *cls)
  771. {
  772. in_shutdown = GNUNET_YES;
  773. if (0 != client_count)
  774. return;
  775. do_shutdown ();
  776. }
  777. /**
  778. * A client disconnected. Remove all of its data structure entries.
  779. *
  780. * @param cls closure, NULL
  781. * @param client identification of the client
  782. * @param app_cls the `struct ClientEntry *`
  783. */
  784. static void
  785. client_disconnect_cb (void *cls,
  786. struct GNUNET_SERVICE_Client *client,
  787. void *app_cls)
  788. {
  789. struct ClientEntry *ce = app_cls;
  790. struct WatchEntry *we;
  791. struct WatchEntry *wen;
  792. struct StatsEntry *pos;
  793. struct SubsystemEntry *se;
  794. client_count--;
  795. for (se = sub_head; NULL != se; se = se->next)
  796. {
  797. for (pos = se->stat_head; NULL != pos; pos = pos->next)
  798. {
  799. wen = pos->we_head;
  800. while (NULL != (we = wen))
  801. {
  802. wen = we->next;
  803. if (we->ce != ce)
  804. continue;
  805. GNUNET_CONTAINER_DLL_remove (pos->we_head, pos->we_tail, we);
  806. GNUNET_free (we);
  807. }
  808. }
  809. }
  810. GNUNET_free (ce);
  811. if ((0 == client_count) && (GNUNET_YES == in_shutdown))
  812. do_shutdown ();
  813. }
  814. /**
  815. * We've read a `struct GNUNET_STATISTICS_SetMessage *` from
  816. * disk. Check that it is well-formed, and if so pass it to
  817. * the handler for set messages.
  818. *
  819. * @param cls NULL
  820. * @param message the message found on disk
  821. * @return #GNUNET_OK on success,
  822. * #GNUNET_NO to stop further processing (no error)
  823. * #GNUNET_SYSERR to stop further processing with error
  824. */
  825. static int
  826. inject_message (void *cls, const struct GNUNET_MessageHeader *message)
  827. {
  828. uint16_t msize = ntohs (message->size);
  829. const struct GNUNET_STATISTICS_SetMessage *sm;
  830. sm = (const struct GNUNET_STATISTICS_SetMessage *) message;
  831. if ((sizeof(struct GNUNET_STATISTICS_SetMessage) > msize) ||
  832. (GNUNET_OK != check_set (NULL, sm)))
  833. {
  834. GNUNET_break (0);
  835. return GNUNET_SYSERR;
  836. }
  837. handle_set (NULL, sm);
  838. return GNUNET_OK;
  839. }
  840. /**
  841. * Load persistent values from disk. Disk format is exactly the same
  842. * format that we also use for setting the values over the network.
  843. */
  844. static void
  845. load ()
  846. {
  847. char *fn;
  848. struct GNUNET_BIO_ReadHandle *rh;
  849. uint64_t fsize;
  850. char *buf;
  851. struct GNUNET_MessageStreamTokenizer *mst;
  852. if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg,
  853. "STATISTICS",
  854. "DATABASE",
  855. &fn))
  856. {
  857. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
  858. "STATISTICS",
  859. "DATABASE");
  860. return;
  861. }
  862. if ((GNUNET_OK !=
  863. GNUNET_DISK_file_size (fn, &fsize, GNUNET_NO, GNUNET_YES)) ||
  864. (0 == fsize))
  865. {
  866. GNUNET_free (fn);
  867. return;
  868. }
  869. buf = GNUNET_malloc (fsize);
  870. rh = GNUNET_BIO_read_open_file (fn);
  871. if (! rh)
  872. {
  873. GNUNET_free (buf);
  874. GNUNET_free (fn);
  875. return;
  876. }
  877. if (GNUNET_OK != GNUNET_BIO_read (rh, fn, buf, fsize))
  878. {
  879. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "read", fn);
  880. GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, NULL));
  881. GNUNET_free (buf);
  882. GNUNET_free (fn);
  883. return;
  884. }
  885. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  886. _ ("Loading %llu bytes of statistics from `%s'\n"),
  887. (unsigned long long) fsize,
  888. fn);
  889. mst = GNUNET_MST_create (&inject_message, NULL);
  890. GNUNET_break (
  891. GNUNET_OK ==
  892. GNUNET_MST_from_buffer (mst, buf, (size_t) fsize, GNUNET_YES, GNUNET_NO));
  893. GNUNET_MST_destroy (mst);
  894. GNUNET_free (buf);
  895. GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, NULL));
  896. GNUNET_free (fn);
  897. }
  898. /**
  899. * Process statistics requests.
  900. *
  901. * @param cls closure
  902. * @param c configuration to use
  903. * @param service the initialized service
  904. */
  905. static void
  906. run (void *cls,
  907. const struct GNUNET_CONFIGURATION_Handle *c,
  908. struct GNUNET_SERVICE_Handle *service)
  909. {
  910. cfg = c;
  911. nc = GNUNET_notification_context_create (16);
  912. load ();
  913. GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
  914. }
  915. /**
  916. * Define "main" method using service macro.
  917. */
  918. GNUNET_SERVICE_MAIN (
  919. "statistics",
  920. GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN,
  921. &run,
  922. &client_connect_cb,
  923. &client_disconnect_cb,
  924. NULL,
  925. GNUNET_MQ_hd_var_size (set,
  926. GNUNET_MESSAGE_TYPE_STATISTICS_SET,
  927. struct GNUNET_STATISTICS_SetMessage,
  928. NULL),
  929. GNUNET_MQ_hd_var_size (get,
  930. GNUNET_MESSAGE_TYPE_STATISTICS_GET,
  931. struct GNUNET_MessageHeader,
  932. NULL),
  933. GNUNET_MQ_hd_var_size (watch,
  934. GNUNET_MESSAGE_TYPE_STATISTICS_WATCH,
  935. struct GNUNET_MessageHeader,
  936. NULL),
  937. GNUNET_MQ_hd_fixed_size (disconnect,
  938. GNUNET_MESSAGE_TYPE_STATISTICS_DISCONNECT,
  939. struct GNUNET_MessageHeader,
  940. NULL),
  941. GNUNET_MQ_handler_end ());
  942. #if defined(__linux__) && defined(__GLIBC__)
  943. #include <malloc.h>
  944. /**
  945. * MINIMIZE heap size (way below 128k) since this process doesn't need much.
  946. */
  947. void __attribute__ ((constructor))
  948. GNUNET_STATISTICS_memory_init ()
  949. {
  950. mallopt (M_TRIM_THRESHOLD, 4 * 1024);
  951. mallopt (M_TOP_PAD, 1 * 1024);
  952. malloc_trim (0);
  953. }
  954. #endif
  955. /* end of gnunet-service-statistics.c */