util.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // This file is part of BOINC.
  2. // http://boinc.berkeley.edu
  3. // Copyright (C) 2008 University of California
  4. //
  5. // BOINC is free software; you can redistribute it and/or modify it
  6. // under the terms of the GNU Lesser General Public License
  7. // as published by the Free Software Foundation,
  8. // either version 3 of the License, or (at your option) any later version.
  9. //
  10. // BOINC is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. // See the GNU Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public License
  16. // along with BOINC. If not, see <http://www.gnu.org/licenses/>.
  17. #ifndef BOINC_UTIL_H
  18. #define BOINC_UTIL_H
  19. #include <stdlib.h>
  20. #include <string>
  21. #include <vector>
  22. extern double dtime();
  23. extern double dday();
  24. extern void boinc_sleep(double);
  25. extern void push_unique(std::string, std::vector<std::string>&);
  26. // NOTE: use #include <functional> to get max,min
  27. #define SECONDS_PER_DAY 86400
  28. #define KILO (1024.)
  29. #define MEGA (1024.*KILO)
  30. #define GIGA (1024.*MEGA)
  31. #define TERA (1024.*GIGA)
  32. static inline double drand() {
  33. return (double)rand()/(double)RAND_MAX;
  34. }
  35. extern double rand_normal();
  36. #ifdef _WIN32
  37. #include "boinc_win.h"
  38. extern int boinc_thread_cpu_time(HANDLE thread_handle, double& cpu);
  39. extern int boinc_process_cpu_time(HANDLE process_handle, double& cpu);
  40. #else
  41. // setpriority(2) arg to run in background
  42. // (don't use 20 because
  43. //
  44. static const int PROCESS_IDLE_PRIORITY = 19;
  45. static const int PROCESS_MEDIUM_PRIORITY = 10;
  46. static const int PROCESS_NORMAL_PRIORITY = 0;
  47. static const int PROCESS_ABOVE_NORMAL_PRIORITY = -10;
  48. static const int PROCESS_HIGH_PRIORITY = -15;
  49. static const int PROCESS_REALTIME_PRIORITY = -20;
  50. extern double linux_cpu_time(int pid);
  51. #endif
  52. extern void update_average(double, double, double, double, double&, double&);
  53. extern int boinc_calling_thread_cpu_time(double&);
  54. inline bool in_vector(int n, std::vector<int>& v) {
  55. for (unsigned int i=0; i<v.size(); i++) {
  56. if (n == v[i]) return true;
  57. }
  58. return false;
  59. }
  60. // fake a crash
  61. //
  62. extern void boinc_crash();
  63. // read files into memory.
  64. // Use only for non-binary files; returns null-terminated string.
  65. //
  66. extern int read_file_malloc(
  67. const char* path, char*& result, size_t max_len=0, bool tail=false
  68. );
  69. extern int read_file_string(
  70. const char* path, std::string& result, size_t max_len=0, bool tail=false
  71. );
  72. #ifdef _WIN32
  73. extern int run_program(
  74. const char* dir, // directory to run program in; NULL if current dir
  75. const char* file, // path of executable
  76. int argc, char *const argv[], // cmdline args, UNIX-style
  77. double, // if nonzero, wait for X seconds, then check
  78. // whether process is still running, return error if not
  79. HANDLE& // process handle
  80. );
  81. extern int kill_program(HANDLE);
  82. extern int kill_program(int, int exit_code=0);
  83. extern int get_exit_status(HANDLE);
  84. extern bool process_exists(HANDLE);
  85. #else
  86. // like Win version, but returns PID
  87. extern int run_program(
  88. const char* dir, const char* file, int argc, char *const argv[], double, int&
  89. );
  90. extern int kill_program(int);
  91. extern int get_exit_status(int);
  92. extern bool process_exists(int);
  93. #endif
  94. extern int wait_client_mutex(const char* dir, double timeout);
  95. extern int get_real_executable_path(char* path, size_t max_len);
  96. #ifdef GCL_SIMULATOR
  97. extern double simtime;
  98. #define time(x) ((int)simtime)
  99. #endif
  100. #endif