benchtest4.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * benchtest4.c
  3. *
  4. *
  5. * --------------------------------------------------------------------------
  6. *
  7. * Pthreads-embedded (PTE) - POSIX Threads Library for embedded systems
  8. * Copyright(C) 2008 Jason Schmidlapp
  9. *
  10. * Contact Email: [email protected]
  11. *
  12. *
  13. * Based upon Pthreads-win32 - POSIX Threads Library for Win32
  14. * Copyright(C) 1998 John E. Bossom
  15. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  16. *
  17. * Contact Email: [email protected]
  18. *
  19. * The original list of contributors to the Pthreads-win32 project
  20. * is contained in the file CONTRIBUTORS.ptw32 included with the
  21. * source code distribution. The list can also be seen at the
  22. * following World Wide Web location:
  23. * http://sources.redhat.com/pthreads-win32/contributors.html
  24. *
  25. * This library is free software; you can redistribute it and/or
  26. * modify it under the terms of the GNU Lesser General Public
  27. * License as published by the Free Software Foundation; either
  28. * version 2 of the License, or (at your option) any later version.
  29. *
  30. * This library is distributed in the hope that it will be useful,
  31. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  33. * Lesser General Public License for more details.
  34. *
  35. * You should have received a copy of the GNU Lesser General Public
  36. * License along with this library in the file COPYING.LIB;
  37. * if not, write to the Free Software Foundation, Inc.,
  38. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  39. *
  40. * --------------------------------------------------------------------------
  41. *
  42. * Measure time taken to complete an elementary operation.
  43. *
  44. * - Mutex
  45. * Single thread iteration over trylock/unlock for each mutex type.
  46. */
  47. #include "test.h"
  48. #ifdef __GNUC__
  49. #include <stdlib.h>
  50. #endif
  51. #include "benchtest.h"
  52. #define PTW32_MUTEX_TYPES
  53. #define ITERATIONS 100000L
  54. static pthread_mutex_t mx;
  55. static pthread_mutexattr_t ma;
  56. static struct _timeb currSysTimeStart;
  57. static struct _timeb currSysTimeStop;
  58. static long durationMilliSecs;
  59. static long overHeadMilliSecs = 0;
  60. #define GetDurationMilliSecs(_TStart, _TStop) ((_TStop.time*1000+_TStop.millitm) \
  61. - (_TStart.time*1000+_TStart.millitm))
  62. /*
  63. * Dummy use of j, otherwise the loop may be removed by the optimiser
  64. * when doing the overhead timing with an empty loop.
  65. */
  66. #define TESTSTART \
  67. { int i, j = 0, k = 0; _ftime(&currSysTimeStart); for (i = 0; i < ITERATIONS; i++) { j++;
  68. #define TESTSTOP \
  69. }; _ftime(&currSysTimeStop); if (j + k == i) j++; }
  70. static void
  71. runTest (char * testNameString, int mType)
  72. {
  73. #ifdef PTW32_MUTEX_TYPES
  74. pthread_mutexattr_settype(&ma, mType);
  75. #endif
  76. pthread_mutex_init(&mx, &ma);
  77. TESTSTART
  78. (void) pthread_mutex_trylock(&mx);
  79. (void) pthread_mutex_unlock(&mx);
  80. TESTSTOP
  81. pthread_mutex_destroy(&mx);
  82. durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
  83. printf( "%-45s %15ld %15.3f\n",
  84. testNameString,
  85. durationMilliSecs,
  86. (float) durationMilliSecs * 1E3 / ITERATIONS);
  87. }
  88. int pthread_test_bench4()
  89. {
  90. pthread_mutexattr_init(&ma);
  91. printf( "=============================================================================\n");
  92. printf( "Trylock plus unlock on an unlocked mutex.\n");
  93. printf( "%ld iterations.\n\n", ITERATIONS);
  94. printf( "%-45s %15s %15s\n",
  95. "Test",
  96. "Total(msec)",
  97. "average(usec)");
  98. /*
  99. * Time the loop overhead so we can subtract it from the actual test times.
  100. */
  101. TESTSTART
  102. TESTSTOP
  103. durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
  104. overHeadMilliSecs = durationMilliSecs;
  105. /*
  106. * Now we can start the actual tests
  107. */
  108. #ifdef PTW32_MUTEX_TYPES
  109. runTest("PTHREAD_MUTEX_DEFAULT", PTHREAD_MUTEX_DEFAULT);
  110. runTest("PTHREAD_MUTEX_NORMAL", PTHREAD_MUTEX_NORMAL);
  111. runTest("PTHREAD_MUTEX_ERRORCHECK", PTHREAD_MUTEX_ERRORCHECK);
  112. runTest("PTHREAD_MUTEX_RECURSIVE", PTHREAD_MUTEX_RECURSIVE);
  113. #else
  114. runTest("Non-blocking lock", 0);
  115. #endif
  116. printf( "=============================================================================\n");
  117. /*
  118. * End of tests.
  119. */
  120. pthread_mutexattr_destroy(&ma);
  121. return 0;
  122. }