parse_env.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #if defined WITH_KNOBS && defined GOOD_FIT
  59. env = getenv("DMMLIB_GOOD_FIT");
  60. if(env != NULL) {
  61. char *end;
  62. double good_fit_perc = strtod(env, &end);
  63. if(good_fit_perc != 0) {
  64. systemallocator.dmm_knobs.fit_percentage = good_fit_perc;
  65. }
  66. }
  67. #endif /* WITH_KNOBS && GOOD_FIT */
  68. #ifdef WITH_KNOBS
  69. env = getenv("DMMLIB_SYSALLOC_SZ");
  70. if(env != NULL) {
  71. char *end;
  72. uintmax_t num = strtoumax(env, &end, 10);
  73. if(num != UINTMAX_MAX) {
  74. systemallocator.dmm_knobs.sys_alloc_size = (double) num;
  75. }
  76. }
  77. #endif /* WITH_KNOBS */
  78. systemallocator.initialized = true;
  79. return;
  80. }
  81. #if defined WITH_MEM_TRACE || defined WITH_STATS_TRACE || defined WITH_DEBUG
  82. __attribute__((destructor)) void close_trace_files(void);
  83. /** Closes the opened trace files. */
  84. void close_trace_files(void) {
  85. #ifdef WITH_MEM_TRACE
  86. if(mem_fd != stderr) {
  87. fclose(mem_fd);
  88. }
  89. #endif /* WITH_MEM_TRACE */
  90. #ifdef WITH_STATS_TRACE
  91. if(stats_fd != stderr) {
  92. fclose(stats_fd);
  93. }
  94. #endif /* WITH_STATS_TRACE */
  95. #ifdef WITH_DEBUG
  96. if(dbg_fd != stderr) {
  97. fclose(dbg_fd);
  98. }
  99. #endif /* WITH_DEBUG */
  100. return;
  101. }
  102. #endif /* WITH_MEM_TRACE || WITH_STATS_TRACE || WITH_DEBUG */