parse_env.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright Institute of Communication and Computer Systems (ICCS)
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. /**
  18. * @file parse_env.c
  19. * @author Ioannis Koutras (joko@microlab.ntua.gr)
  20. * @date November 2012
  21. * @brief Parse the DMMLIB_OPTS enviroment variable.
  22. */
  23. #include <inttypes.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <stdio.h>
  27. #include "dmmlib/config.h"
  28. #include "dmmlib/dmmlib.h"
  29. #include "dmmlib_trace.h"
  30. __attribute__((constructor)) void parse_env(void);
  31. /** Parses the DMMLIB_OPTS enviroment variable. */
  32. void parse_env(void) {
  33. const char* env;
  34. #ifdef WITH_MEM_TRACE
  35. env = getenv("DMMLIB_MEM_TRACE");
  36. if(env == NULL) {
  37. mem_fd = stderr;
  38. } else {
  39. mem_fd = fopen(env, "a+");
  40. }
  41. #endif /* WITH_MEM_TRACE */
  42. #ifdef WITH_STATS_TRACE
  43. env = getenv("DMMLIB_STATS_TRACE");
  44. if(env == NULL) {
  45. stats_fd = stderr;
  46. } else {
  47. stats_fd = fopen(env, "a+");
  48. }
  49. #endif /* WITH_STATS_TRACE */
  50. #ifdef WITH_DEBUG
  51. env = getenv("DMMLIB_DBG_TRACE");
  52. if(env == NULL) {
  53. dbg_fd = stderr;
  54. } else {
  55. dbg_fd = fopen(env, "a+");
  56. }
  57. #endif /* WITH_DEBUG */
  58. env = getenv("DMMLIB_SYSALLOC_SZ");
  59. if(env != NULL) {
  60. char *end;
  61. uintmax_t num = strtoumax(env, &end, 10);
  62. if(num != UINTMAX_MAX) {
  63. systemallocator.dmm_knobs.sys_alloc_size = (double) num;
  64. }
  65. }
  66. systemallocator.initialized = true;
  67. return;
  68. }
  69. #if defined WITH_MEM_TRACE || defined WITH_STATS_TRACE || defined WITH_DEBUG
  70. __attribute__((destructor)) void close_trace_files(void);
  71. /** Closes the opened trace files. */
  72. void close_trace_files(void) {
  73. #ifdef WITH_MEM_TRACE
  74. if(mem_fd != stderr) {
  75. fclose(mem_fd);
  76. }
  77. #endif /* WITH_MEM_TRACE */
  78. #ifdef WITH_STATS_TRACE
  79. if(stats_fd != stderr) {
  80. fclose(stats_fd);
  81. }
  82. #endif /* WITH_STATS_TRACE */
  83. #ifdef WITH_DEBUG
  84. if(dbg_fd != stderr) {
  85. fclose(dbg_fd);
  86. }
  87. #endif /* WITH_DEBUG */
  88. return;
  89. }
  90. #endif /* WITH_MEM_TRACE || WITH_STATS_TRACE || WITH_DEBUG */