/* mm: common.c Copyright (C) 2006 Macaulay Institute This file is part of mm, a series of programs to test the effect of various mechanisms for mitigating against floating point issues on memory usage and time. mm is free software; you can redistributed it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. mm is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. (LICENCE file in this directory.) You should have received a copy of the GNU General Public License along with this programl if not, write to the Free Software Fountation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Contact information: Gary Polhill, Macaulay Institute, Craigiebuckler, Aberdeen, AB15 8QH. United Kingdom g.polhill@macaulay.ac.uk */ #include #include #include #include #include #include #include #include #include #include #include #include #include /** * This file contains definitions of common functionality for all * implementations of mm. */ /** * die */ void die(const char *msg, ...) { va_list ap; va_start(ap, msg); vfprintf(stderr, msg, ap); va_end(ap); abort(); } /** * set_seed */ void set_seed(const char *seed_str) { long seed; if(strcmp(seed_str, "TIME") == 0) { seed = (long)time(NULL); } else { const char *p; p = seed_str; while((*p) != '\0') { if(!isdigit(*p)) { die("Invalid seed %s (not a number or the word \"TIME\"\n", seed_str); } p++; } seed = atol(seed_str); } srand48(seed); } /** * open_process_info */ int open_process_info(void) { int fd; fd = open("/proc/self/psinfo", O_RDONLY); if(fd == -1) { die("Error opening process info: %s\n", strerror(errno)); } return fd; } /** * read_process_info */ void *read_process_info(int fd) { void *p; p = malloc(sizeof(psinfo_t)); if(p == NULL) { die("Memory allocation error: %s\n", strerror(errno)); } if(pread(fd, p, sizeof(psinfo_t), 0) == -1) { die("Error reading process info: %s\n", strerror(errno)); } return p; } /** * get_time_stamp */ long double get_time_stamp(void *p) { psinfo_t info; memcpy(&info, p, sizeof(psinfo_t)); return (long double)info.pr_time.tv_sec + ((long double)info.pr_time.tv_nsec * 1e-9L); } /** * get_memory_usage */ long get_memory_usage(void *p) { psinfo_t info; memcpy(&info, p, sizeof(psinfo_t)); return (long)info.pr_size; } /** * get_pr_pid */ long get_pr_pid(void *p) { psinfo_t info; memcpy(&info, p, sizeof(psinfo_t)); return (long)info.pr_pid; } /** * get_pr_ppid */ long get_pr_ppid(void *p) { psinfo_t info; memcpy(&info, p, sizeof(psinfo_t)); return (long)info.pr_ppid; } /** * get_pr_uid */ long get_pr_uid(void *p) { psinfo_t info; memcpy(&info, p, sizeof(psinfo_t)); return (long)info.pr_uid; } /** * get_pr_euid */ long get_pr_euid(void *p) { psinfo_t info; memcpy(&info, p, sizeof(psinfo_t)); return (long)info.pr_euid; } /** * get_pr_name */ const char *get_pr_name(void *p) { psinfo_t *info; memcpy(&info, &p, sizeof(psinfo_t *)); return info->pr_fname; } /** * get_pr_argc */ int get_pr_argc(void *p) { psinfo_t info; memcpy(&info, p, sizeof(psinfo_t)); return info.pr_argc; } /** * free_process_info */ void free_process_info(void *p) { free(p); } /** * close_process_info */ void close_process_info(int fd) { close(fd); }