detailed-latency.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2019 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. #include "private-lib-core.h"
  25. int
  26. lws_det_lat_active(struct lws_context *context)
  27. {
  28. return !!context->detailed_latency_cb;
  29. }
  30. int
  31. lws_det_lat_cb(struct lws_context *context, lws_detlat_t *d)
  32. {
  33. int n;
  34. if (!context->detailed_latency_cb)
  35. return 0;
  36. n = context->detailed_latency_cb(context, d);
  37. memset(&d->latencies, 0, sizeof(d->latencies));
  38. return n;
  39. }
  40. static const char types[] = "rwNCTt????";
  41. int
  42. lws_det_lat_plot_cb(struct lws_context *context, const lws_detlat_t *d)
  43. {
  44. char buf[80], *p = buf, *end = &p[sizeof(buf) - 1];
  45. if (!context->detailed_latency_filepath)
  46. return 1;
  47. if (context->latencies_fd == -1) {
  48. context->latencies_fd = open(context->detailed_latency_filepath,
  49. LWS_O_CREAT | LWS_O_TRUNC | LWS_O_WRONLY, 0600);
  50. if (context->latencies_fd == -1)
  51. return 1;
  52. }
  53. p += lws_snprintf(p, lws_ptr_diff(end, p),
  54. "%llu %c %u %u %u %u %u %zu %zu\n",
  55. (unsigned long long)lws_now_usecs(), types[d->type],
  56. d->latencies[LAT_DUR_PROXY_CLIENT_REQ_TO_WRITE],
  57. d->latencies[LAT_DUR_PROXY_CLIENT_WRITE_TO_PROXY_RX],
  58. d->latencies[LAT_DUR_PROXY_PROXY_REQ_TO_WRITE],
  59. d->latencies[LAT_DUR_PROXY_CLIENT_REQ_TO_WRITE] +
  60. d->latencies[LAT_DUR_PROXY_CLIENT_WRITE_TO_PROXY_RX] +
  61. d->latencies[LAT_DUR_PROXY_PROXY_REQ_TO_WRITE],
  62. d->latencies[LAT_DUR_PROXY_RX_TO_ONWARD_TX],
  63. d->acc_size, d->req_size);
  64. write(context->latencies_fd, buf, lws_ptr_diff(p, buf));
  65. return 0;
  66. }