fork.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*++
  2. Copyright (c) 2015 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. fork.c
  5. Abstract:
  6. This module implements the performance benchmark tests for the fork()
  7. C library call.
  8. Author:
  9. Chris Stevens 27-Apr-2015
  10. Environment:
  11. User
  12. --*/
  13. //
  14. // ------------------------------------------------------------------- Includes
  15. //
  16. #include <stdlib.h>
  17. #include <errno.h>
  18. #include <unistd.h>
  19. #include <sys/wait.h>
  20. #include "perftest.h"
  21. //
  22. // ---------------------------------------------------------------- Definitions
  23. //
  24. //
  25. // ------------------------------------------------------ Data Type Definitions
  26. //
  27. //
  28. // ----------------------------------------------- Internal Function Prototypes
  29. //
  30. //
  31. // -------------------------------------------------------------------- Globals
  32. //
  33. //
  34. // ------------------------------------------------------------------ Functions
  35. //
  36. void
  37. ForkMain (
  38. PPT_TEST_INFORMATION Test,
  39. PPT_TEST_RESULT Result
  40. )
  41. /*++
  42. Routine Description:
  43. This routine performs the fork performance benchmark test.
  44. Arguments:
  45. Test - Supplies a pointer to the performance test being executed.
  46. Result - Supplies a pointer to a performance test result structure that
  47. receives the tests results.
  48. Return Value:
  49. None.
  50. --*/
  51. {
  52. pid_t Child;
  53. unsigned long long Iterations;
  54. int Status;
  55. Iterations = 0;
  56. Result->Type = PtResultIterations;
  57. Result->Status = 0;
  58. //
  59. // Start the test. This snaps resource usage and starts the clock ticking.
  60. //
  61. Status = PtStartTimedTest(Test->Duration);
  62. if (Status != 0) {
  63. Result->Status = errno;
  64. goto MainEnd;
  65. }
  66. //
  67. // Measure the performance of the fork() C library routine by counting the
  68. // number of times a forked child can be waited on during the given
  69. // duration. The child, in this case, exits immediately.
  70. //
  71. while (PtIsTimedTestRunning() != 0) {
  72. Child = fork();
  73. if (Child < 0) {
  74. Result->Status = errno;
  75. break;
  76. } else if (Child == 0) {
  77. exit(0);
  78. } else {
  79. Child = waitpid(Child, &Status, 0);
  80. if (Child == -1) {
  81. if (PtIsTimedTestRunning() == 0) {
  82. break;
  83. }
  84. Result->Status = errno;
  85. break;
  86. }
  87. if (Status != 0) {
  88. Result->Status = WEXITSTATUS(Status);
  89. break;
  90. }
  91. Iterations += 1;
  92. }
  93. }
  94. Status = PtFinishTimedTest(Result);
  95. if ((Status != 0) && (Result->Status == 0)) {
  96. Result->Status = errno;
  97. }
  98. MainEnd:
  99. Result->Data.Iterations = Iterations;
  100. return;
  101. }
  102. //
  103. // --------------------------------------------------------- Internal Functions
  104. //