/codebase/apps/procmap/src/test_procmap/test_mapper.c

https://github.com/NCAR/lrose-core · C · 240 lines · 116 code · 60 blank · 64 comment · 18 complexity · b94046df5669fcc3c8aa2f76ca21f789 MD5 · raw file

  1. /* *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* */
  2. /* ** Copyright UCAR (c) 1990 - 2016 */
  3. /* ** University Corporation for Atmospheric Research (UCAR) */
  4. /* ** National Center for Atmospheric Research (NCAR) */
  5. /* ** Boulder, Colorado, USA */
  6. /* ** BSD licence applies - redistribution and use in source and binary */
  7. /* ** forms, with or without modification, are permitted provided that */
  8. /* ** the following conditions are met: */
  9. /* ** 1) If the software is modified to produce derivative works, */
  10. /* ** such modified software should be clearly marked, so as not */
  11. /* ** to confuse it with the version available from UCAR. */
  12. /* ** 2) Redistributions of source code must retain the above copyright */
  13. /* ** notice, this list of conditions and the following disclaimer. */
  14. /* ** 3) Redistributions in binary form must reproduce the above copyright */
  15. /* ** notice, this list of conditions and the following disclaimer in the */
  16. /* ** documentation and/or other materials provided with the distribution. */
  17. /* ** 4) Neither the name of UCAR nor the names of its contributors, */
  18. /* ** if any, may be used to endorse or promote products derived from */
  19. /* ** this software without specific prior written permission. */
  20. /* ** DISCLAIMER: THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS */
  21. /* ** OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
  22. /* ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. */
  23. /* *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* */
  24. /**************************************************************************
  25. * test_mapper.c :
  26. *
  27. * Mike Dixon
  28. *
  29. * RAP NCAR Boulder Colorado USA
  30. *
  31. * Feb 1996
  32. *
  33. **************************************************************************/
  34. #include "test_procmap.h"
  35. #include <toolsa/sockutil.h>
  36. #include <sys/times.h>
  37. #include <dataport/port_types.h>
  38. #ifdef NOT_NOW
  39. static int connection_good()
  40. {
  41. int fd;
  42. fprintf(stderr, "Trying .....\n");
  43. if ((fd = SKU_open_client(Glob->procmap_host, 5433)) >= 0) {
  44. fprintf(stderr, "Connection to %s, port %d, established\n",
  45. Glob->procmap_host, 5433);
  46. SKU_close(fd);
  47. return (TRUE);
  48. } else {
  49. fprintf(stderr, "Unable to open connection to %s, port 5433\n",
  50. Glob->procmap_host);
  51. return (FALSE);
  52. }
  53. }
  54. #endif
  55. static void pr_times(clock_t real, struct tms *start, struct tms *end)
  56. {
  57. static long clktck = 0;
  58. static double dtck;
  59. if (clktck == 0) {
  60. if ((clktck = sysconf(_SC_CLK_TCK)) < 0) {
  61. fprintf(stderr, "Error getting clock tick constant\n");
  62. clktck = 1;
  63. }
  64. dtck = (double) clktck;
  65. }
  66. fprintf(stdout, " real: %10.5f\n", real / dtck);
  67. fprintf(stdout, " user: %10.5f\n",
  68. (end->tms_utime - start->tms_utime) / dtck);
  69. fprintf(stdout, " sys: %10.5f\n",
  70. (end->tms_stime - start->tms_stime) / dtck);
  71. }
  72. static void register_proc(void)
  73. {
  74. /* auto_init does the reg for us */
  75. PMU_auto_init(Glob->name, Glob->instance, Glob->max_reg_int);
  76. }
  77. /*
  78. * get_info : get the proc info from the proc mapper
  79. *
  80. */
  81. static int get_info(PROCMAP_info_t **return_info_p)
  82. {
  83. int i, retval;
  84. clock_t start, end;
  85. struct tms tms_start, tms_end;
  86. int nprocs;
  87. long uptime;
  88. PROCMAP_request_t request;
  89. PROCMAP_info_t *return_info;
  90. /*
  91. * init times
  92. */
  93. if ((start = times(&tms_start)) == -1) {
  94. fprintf(stderr, "times error");
  95. }
  96. /*
  97. * load up request struct
  98. */
  99. memset ((void *) &request,
  100. (int) 0, (size_t) sizeof(PROCMAP_request_t));
  101. strncpy(request.name, Glob->name, PROCMAP_NAME_MAX);
  102. strncpy(request.instance, Glob->instance,
  103. PROCMAP_INSTANCE_MAX);
  104. nprocs = 0;
  105. retval = PMU_requestInfo(&request, &nprocs, &uptime,
  106. return_info_p, Glob->procmap_host, "none");
  107. if (retval == 0) {
  108. fprintf(stdout, "No procs found\n");
  109. SKU_set_headers_to_new();
  110. return (-1);
  111. }
  112. if (nprocs == 0) {
  113. SKU_set_headers_to_new();
  114. fprintf(stdout, "No proc mapper found\n");
  115. return (-1);
  116. }
  117. fprintf(stdout, "Request successful, %d procs\n", nprocs);
  118. return_info = *return_info_p;
  119. for (i = 0; i < nprocs; i++) {
  120. fprintf(stdout, "Proc %d: %s %d %s %s %s\n",
  121. i,
  122. return_info[i].host,
  123. return_info[i].pid,
  124. return_info[i].name,
  125. return_info[i].instance,
  126. return_info[i].status_str);
  127. }
  128. fflush(stdout);
  129. if ((end = times(&tms_end)) == -1) {
  130. fprintf(stderr, "times error");
  131. }
  132. fprintf(stdout, "QUERYING INFO: TIMES\n");
  133. pr_times(end-start, &tms_start, &tms_end);
  134. fprintf(stdout, "\n");
  135. /*
  136. * set sockutil headers to new in case they were set to old by
  137. * the proc mapper
  138. */
  139. SKU_set_headers_to_new();
  140. return (0);
  141. }
  142. int test_mapper(void)
  143. /*
  144. * returns 0 on success, -1 on failure
  145. */
  146. {
  147. int retval;
  148. PROCMAP_info_t *return_info;
  149. /*
  150. * first test the connection
  151. */
  152. #ifdef NOT_NOW
  153. if (connection_good()) {
  154. #endif
  155. /*
  156. * register test proc
  157. */
  158. if (Glob->do_register) {
  159. fprintf(stdout, "Registering ...\n");
  160. register_proc();
  161. }
  162. /*
  163. * give the process mapper a little time to register
  164. * the process
  165. */
  166. sleep(1);
  167. /*
  168. * then get test proc's info
  169. */
  170. retval = get_info(&return_info);
  171. if (retval) {
  172. return (-1);
  173. }
  174. return (0);
  175. #ifdef NOT_NOW
  176. } else {
  177. return (-1);
  178. } /* if (connection_good ... */
  179. #endif
  180. }