2008-08-06 18:36:30 +00:00
|
|
|
// This file is part of BOINC.
|
2005-01-20 23:22:22 +00:00
|
|
|
// http://boinc.berkeley.edu
|
2008-08-06 18:36:30 +00:00
|
|
|
// Copyright (C) 2008 University of California
|
2004-11-14 08:29:32 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is free software; you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU Lesser General Public License
|
|
|
|
// as published by the Free Software Foundation,
|
|
|
|
// either version 3 of the License, or (at your option) any later version.
|
2003-07-02 20:57:59 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
2005-01-20 23:22:22 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
// See the GNU Lesser General Public License for more details.
|
2002-04-30 22:22:54 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2003-07-02 02:02:18 +00:00
|
|
|
#ifndef UTIL_H
|
|
|
|
#define UTIL_H
|
|
|
|
|
2012-07-02 18:51:02 +00:00
|
|
|
#include <stdlib.h>
|
2003-09-02 23:26:38 +00:00
|
|
|
#include <string>
|
2007-01-23 22:29:19 +00:00
|
|
|
#include <vector>
|
2003-06-10 18:50:43 +00:00
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
extern double dtime();
|
2005-04-04 06:02:36 +00:00
|
|
|
extern double dday();
|
2003-03-11 22:18:01 +00:00
|
|
|
extern void boinc_sleep(double);
|
2007-01-23 22:29:19 +00:00
|
|
|
extern void push_unique(std::string, std::vector<std::string>&);
|
2006-09-26 16:22:36 +00:00
|
|
|
|
2003-07-03 00:48:43 +00:00
|
|
|
// NOTE: use #include <functional> to get max,min
|
2002-08-09 21:43:19 +00:00
|
|
|
|
2002-09-16 20:11:52 +00:00
|
|
|
#define SECONDS_PER_DAY 86400
|
2014-01-15 07:30:08 +00:00
|
|
|
#define KILO (1024.)
|
|
|
|
#define MEGA (1024.*KILO)
|
|
|
|
#define GIGA (1024.*MEGA)
|
|
|
|
#define TERA (1024.*GIGA)
|
2003-06-09 23:50:49 +00:00
|
|
|
|
|
|
|
static inline double drand() {
|
|
|
|
return (double)rand()/(double)RAND_MAX;
|
|
|
|
}
|
2010-12-09 00:32:50 +00:00
|
|
|
extern double rand_normal();
|
2003-06-09 23:50:49 +00:00
|
|
|
|
2004-02-04 19:14:39 +00:00
|
|
|
#ifdef _WIN32
|
2014-05-08 20:46:26 +00:00
|
|
|
#include "boinc_win.h"
|
2004-12-10 19:17:32 +00:00
|
|
|
extern int boinc_thread_cpu_time(HANDLE thread_handle, double& cpu);
|
2009-03-27 21:03:41 +00:00
|
|
|
extern int boinc_process_cpu_time(HANDLE process_handle, double& cpu);
|
2007-03-20 19:55:53 +00:00
|
|
|
#else
|
|
|
|
// setpriority(2) arg to run in background
|
|
|
|
// (don't use 20 because
|
2007-10-05 16:47:07 +00:00
|
|
|
//
|
2007-03-20 19:55:53 +00:00
|
|
|
static const int PROCESS_IDLE_PRIORITY = 19;
|
2008-11-14 22:08:50 +00:00
|
|
|
static const int PROCESS_MEDIUM_PRIORITY = 10;
|
2014-03-13 16:39:01 +00:00
|
|
|
static const int PROCESS_NORMAL_PRIORITY = 0;
|
2015-09-28 17:36:18 +00:00
|
|
|
static const int PROCESS_ABOVE_NORMAL_PRIORITY = -10;
|
|
|
|
static const int PROCESS_HIGH_PRIORITY = -15;
|
|
|
|
static const int PROCESS_REALTIME_PRIORITY = -20;
|
2007-03-20 23:12:22 +00:00
|
|
|
extern double linux_cpu_time(int pid);
|
2004-02-04 19:14:39 +00:00
|
|
|
#endif
|
|
|
|
|
2010-10-29 23:41:34 +00:00
|
|
|
extern void update_average(double, double, double, double, double&, double&);
|
2004-04-07 06:51:42 +00:00
|
|
|
|
2004-12-05 23:52:17 +00:00
|
|
|
extern int boinc_calling_thread_cpu_time(double&);
|
2004-11-05 19:52:19 +00:00
|
|
|
|
2012-12-12 00:05:40 +00:00
|
|
|
inline bool in_vector(int n, std::vector<int>& v) {
|
|
|
|
for (unsigned int i=0; i<v.size(); i++) {
|
|
|
|
if (n == v[i]) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-05-01 18:25:17 +00:00
|
|
|
// fake a crash
|
|
|
|
//
|
2007-05-23 21:02:19 +00:00
|
|
|
extern void boinc_crash();
|
2009-05-01 18:25:17 +00:00
|
|
|
|
|
|
|
// read files into memory.
|
|
|
|
// Use only for non-binary files; returns null-terminated string.
|
|
|
|
//
|
|
|
|
extern int read_file_malloc(
|
|
|
|
const char* path, char*& result, size_t max_len=0, bool tail=false
|
|
|
|
);
|
|
|
|
extern int read_file_string(
|
|
|
|
const char* path, std::string& result, size_t max_len=0, bool tail=false
|
|
|
|
);
|
2006-12-14 00:42:43 +00:00
|
|
|
|
2008-03-10 22:36:22 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
extern int run_program(
|
2014-01-21 07:34:35 +00:00
|
|
|
const char* dir, // directory to run program in; NULL if current dir
|
|
|
|
const char* file, // path of executable
|
|
|
|
int argc, char *const argv[], // cmdline args, UNIX-style
|
|
|
|
double, // if nonzero, wait for X seconds, then check
|
|
|
|
// whether process is still running, return error if not
|
|
|
|
HANDLE& // process handle
|
2008-03-10 22:36:22 +00:00
|
|
|
);
|
|
|
|
|
2014-07-29 20:19:07 +00:00
|
|
|
extern int kill_program(HANDLE);
|
|
|
|
extern int kill_program(int, int exit_code=0);
|
2008-03-10 22:36:22 +00:00
|
|
|
extern int get_exit_status(HANDLE);
|
|
|
|
extern bool process_exists(HANDLE);
|
|
|
|
|
|
|
|
#else
|
2014-01-21 07:34:35 +00:00
|
|
|
// like Win version, but returns PID
|
2008-03-10 22:36:22 +00:00
|
|
|
extern int run_program(
|
2010-10-13 21:02:42 +00:00
|
|
|
const char* dir, const char* file, int argc, char *const argv[], double, int&
|
2008-03-10 22:36:22 +00:00
|
|
|
);
|
2014-07-29 20:19:07 +00:00
|
|
|
extern int kill_program(int);
|
2008-03-10 22:36:22 +00:00
|
|
|
extern int get_exit_status(int);
|
|
|
|
extern bool process_exists(int);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
extern int wait_client_mutex(const char* dir, double timeout);
|
2008-09-22 20:33:59 +00:00
|
|
|
|
|
|
|
#ifdef GCL_SIMULATOR
|
|
|
|
extern double simtime;
|
|
|
|
#define time(x) ((int)simtime)
|
|
|
|
#endif
|
|
|
|
|
2003-06-10 18:50:43 +00:00
|
|
|
#endif
|