msg_create.libos.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* Test to create 100 message queues and query them from another process*/
  2. #include <shim_unistd.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/ipc.h>
  7. #include <sys/msg.h>
  8. #include <sys/time.h>
  9. #include <sys/types.h>
  10. #include <sys/wait.h>
  11. #include <unistd.h>
  12. struct msg_buf {
  13. long mtype;
  14. char msgtext[512];
  15. };
  16. #define TEST_TIMES 1000
  17. #define DO_BENCH 1
  18. int create_q(int key) {
  19. int r = msgget(key, IPC_CREAT | 0600);
  20. #ifndef DO_BENCH
  21. printf("The identifier used is %d\n", r);
  22. #endif
  23. if (r < 0) {
  24. perror("msgget\n");
  25. exit(-1);
  26. }
  27. #ifndef DO_BENCH
  28. else
  29. printf("Created a message queue\n");
  30. #endif
  31. return r;
  32. }
  33. int connect_q(int key) {
  34. int r = msgget(key, 0);
  35. #ifndef DO_BENCH
  36. printf("The identifier used is %d\n", r);
  37. #endif
  38. if (r < 0) {
  39. perror("msgget");
  40. exit(-1);
  41. }
  42. #ifndef DO_BENCH
  43. else
  44. printf("Connected the message queue\n");
  45. #endif
  46. return r;
  47. }
  48. int keys[TEST_TIMES];
  49. int ids[TEST_TIMES];
  50. /* server always creates queues */
  51. void server(void) {
  52. struct timeval tv1, tv2;
  53. int i;
  54. gettimeofday(&tv1, NULL);
  55. for (i = 0; i < TEST_TIMES; i++) {
  56. ids[i] = create_q(keys[i]);
  57. }
  58. for (i = 0; i < TEST_TIMES; i++) {
  59. msgpersist(ids[i], MSGPERSIST_STORE);
  60. }
  61. gettimeofday(&tv2, NULL);
  62. printf("time spent on %d creation: %llu microsecond\n", TEST_TIMES,
  63. (tv2.tv_sec * 1000000ULL + tv2.tv_usec) - (tv1.tv_sec * 1000000ULL + tv1.tv_usec));
  64. }
  65. /* client always connects queues */
  66. void client(void) {
  67. struct timeval tv1, tv2;
  68. int i;
  69. gettimeofday(&tv1, NULL);
  70. for (i = 0; i < TEST_TIMES; i++) {
  71. msgpersist(ids[i], MSGPERSIST_LOAD);
  72. }
  73. gettimeofday(&tv2, NULL);
  74. printf("time spent on %d connection: %llu microsecond\n", TEST_TIMES,
  75. (tv2.tv_sec * 1000000ULL + tv2.tv_usec) - (tv1.tv_sec * 1000000ULL + tv1.tv_usec));
  76. for (i = 0; i < TEST_TIMES; i++) {
  77. msgctl(ids[i], IPC_RMID, NULL);
  78. }
  79. }
  80. int main(int argc, char** argv) {
  81. for (int i = 0; i < TEST_TIMES; i++) {
  82. keys[i] = rand();
  83. }
  84. server();
  85. client();
  86. return 0;
  87. }