1
0

benchmark-async-pummel.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include "task.h"
  22. #include "uv.h"
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #define NUM_PINGS (1000 * 1000)
  26. #define ACCESS_ONCE(type, var) (*(volatile type*) &(var))
  27. static unsigned int callbacks;
  28. static volatile int done;
  29. static const char running[] = "running";
  30. static const char stop[] = "stop";
  31. static const char stopped[] = "stopped";
  32. static void async_cb(uv_async_t* handle, int status) {
  33. if (++callbacks == NUM_PINGS) {
  34. /* Tell the pummel thread to stop. */
  35. ACCESS_ONCE(const char*, handle->data) = stop;
  36. /* Wait for for the pummel thread to acknowledge that it has stoppped. */
  37. while (ACCESS_ONCE(const char*, handle->data) != stopped)
  38. uv_sleep(0);
  39. uv_close((uv_handle_t*) handle, NULL);
  40. }
  41. }
  42. static void pummel(void* arg) {
  43. uv_async_t* handle = (uv_async_t*) arg;
  44. while (ACCESS_ONCE(const char*, handle->data) == running)
  45. uv_async_send(handle);
  46. /* Acknowledge that we've seen handle->data change. */
  47. ACCESS_ONCE(const char*, handle->data) = stopped;
  48. }
  49. static int test_async_pummel(int nthreads) {
  50. uv_thread_t* tids;
  51. uv_async_t handle;
  52. uint64_t time;
  53. int i;
  54. tids = calloc(nthreads, sizeof(tids[0]));
  55. ASSERT(tids != NULL);
  56. ASSERT(0 == uv_async_init(uv_default_loop(), &handle, async_cb));
  57. ACCESS_ONCE(const char*, handle.data) = running;
  58. for (i = 0; i < nthreads; i++)
  59. ASSERT(0 == uv_thread_create(tids + i, pummel, &handle));
  60. time = uv_hrtime();
  61. ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
  62. time = uv_hrtime() - time;
  63. done = 1;
  64. for (i = 0; i < nthreads; i++)
  65. ASSERT(0 == uv_thread_join(tids + i));
  66. printf("async_pummel_%d: %s callbacks in %.2f seconds (%s/sec)\n",
  67. nthreads,
  68. fmt(callbacks),
  69. time / 1e9,
  70. fmt(callbacks / (time / 1e9)));
  71. free(tids);
  72. MAKE_VALGRIND_HAPPY();
  73. return 0;
  74. }
  75. BENCHMARK_IMPL(async_pummel_1) {
  76. return test_async_pummel(1);
  77. }
  78. BENCHMARK_IMPL(async_pummel_2) {
  79. return test_async_pummel(2);
  80. }
  81. BENCHMARK_IMPL(async_pummel_4) {
  82. return test_async_pummel(4);
  83. }
  84. BENCHMARK_IMPL(async_pummel_8) {
  85. return test_async_pummel(8);
  86. }