Reid Spencer | 27dafe1 | 2004-09-11 04:56:56 +0000 | [diff] [blame] | 1 | //===- Unix/Process.cpp - Unix Process Implementation --------- -*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Reid Spencer and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides the generic Unix implementation of the Process class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame^] | 14 | #include "Unix.h" |
| 15 | #ifdef HAVE_SYS_TIME_H |
| 16 | #include <sys/time.h> |
| 17 | #endif |
| 18 | #ifdef HAVE_SYS_RESOURCE_H |
| 19 | #include <sys/resource.h> |
| 20 | #endif |
| 21 | #ifdef HAVE_MALLOC_H |
| 22 | #include <malloc.h> |
| 23 | #endif |
Reid Spencer | 27dafe1 | 2004-09-11 04:56:56 +0000 | [diff] [blame] | 24 | |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | //=== WARNING: Implementation here must contain only generic UNIX code that |
| 27 | //=== is guaranteed to work on *all* UNIX variants. |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
| 30 | namespace llvm { |
| 31 | using namespace sys; |
| 32 | |
| 33 | unsigned |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame^] | 34 | Process::GetPageSize() |
| 35 | { |
| 36 | #if defined(HAVE_GETPAGESIZE) |
| 37 | static const int page_size = ::getpagesize(); |
| 38 | #elif defined(HAVE_SYSCONF) |
| 39 | static long page_size = ::sysconf(_SC_PAGE_SIZE); |
| 40 | #else |
| 41 | #warning Cannot get the page size on this machine |
| 42 | #endif |
Reid Spencer | 27dafe1 | 2004-09-11 04:56:56 +0000 | [diff] [blame] | 43 | return static_cast<unsigned>(page_size); |
| 44 | } |
| 45 | |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame^] | 46 | #if defined(HAVE_SBRK) |
| 47 | static char* som = reinterpret_cast<char*>(::sbrk(0)); |
| 48 | #endif |
| 49 | |
| 50 | uint64_t |
| 51 | Process::GetMallocUsage() |
| 52 | { |
| 53 | #ifdef HAVE_MALLINFO |
| 54 | struct mallinfo mi; |
| 55 | mi = ::mallinfo(); |
| 56 | return mi.uordblks; |
| 57 | #elif HAVE_SBRK |
| 58 | // Note this is only an approximation and more closely resembles |
| 59 | // the value returned by mallinfo in the arena field. |
| 60 | char * eom = sbrk(0); |
| 61 | if (eom != ((char*)-1) && som != ((char*)-1)) |
| 62 | return eom - som; |
| 63 | else |
| 64 | return 0; |
| 65 | #else |
| 66 | #warning Cannot get malloc info on this platform |
| 67 | return 0; |
| 68 | #endif |
| 69 | } |
| 70 | |
| 71 | uint64_t |
| 72 | Process::GetTotalMemoryUsage() |
| 73 | { |
| 74 | #ifdef HAVE_MALLINFO |
| 75 | struct mallinfo mi = ::mallinfo(); |
| 76 | return mi.uordblks + mi.hblkhd; |
| 77 | #else |
| 78 | #warning Cannot get total memory size on this platform |
| 79 | return 0; |
| 80 | #endif |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | Process::GetTimeUsage(TimeValue& elapsed, TimeValue& user_time, |
| 85 | TimeValue& sys_time) |
| 86 | { |
| 87 | elapsed = TimeValue::now(); |
| 88 | #ifdef HAVE_GETRUSAGE |
| 89 | struct rusage usage; |
| 90 | ::getrusage(RUSAGE_SELF, &usage); |
| 91 | user_time.seconds( usage.ru_utime.tv_sec ); |
| 92 | user_time.microseconds( usage.ru_utime.tv_usec ); |
| 93 | sys_time.seconds( usage.ru_stime.tv_sec ); |
| 94 | sys_time.microseconds( usage.ru_stime.tv_usec ); |
| 95 | #else |
| 96 | #warning Cannot get usage times on this platform |
| 97 | user_time.seconds(0); |
| 98 | user_time.microseconds(0); |
| 99 | sys_time.seconds(0); |
| 100 | sys_time.microseconds(0); |
| 101 | #endif |
| 102 | } |
| 103 | |
| 104 | |
Reid Spencer | 27dafe1 | 2004-09-11 04:56:56 +0000 | [diff] [blame] | 105 | } |
| 106 | // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab |