| 1 | /*------------------------------------------------------------\ |
|---|
| 2 | | | |
|---|
| 3 | | Tool : systemcass | |
|---|
| 4 | | | |
|---|
| 5 | | File : usage.h | |
|---|
| 6 | | | |
|---|
| 7 | | Author : Buchmann Richard | |
|---|
| 8 | | | |
|---|
| 9 | | Date : ??_??_2005 | |
|---|
| 10 | | | |
|---|
| 11 | \------------------------------------------------------------*/ |
|---|
| 12 | |
|---|
| 13 | /* |
|---|
| 14 | * This file is part of the Disydent Project |
|---|
| 15 | * Copyright (C) Laboratoire LIP6 - Département ASIM |
|---|
| 16 | * Universite Pierre et Marie Curie |
|---|
| 17 | * |
|---|
| 18 | * Home page : http://www-asim.lip6.fr/disydent |
|---|
| 19 | * E-mail : mailto:richard.buchmann@lip6.fr |
|---|
| 20 | * |
|---|
| 21 | * This library is free software; you can redistribute it and/or modify it |
|---|
| 22 | * under the terms of the GNU Library General Public License as published |
|---|
| 23 | * by the Free Software Foundation; either version 2 of the License, or (at |
|---|
| 24 | * your option) any later version. |
|---|
| 25 | * |
|---|
| 26 | * Disydent is distributed in the hope that it will be |
|---|
| 27 | * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 28 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
|---|
| 29 | * Public License for more details. |
|---|
| 30 | * |
|---|
| 31 | * You should have received a copy of the GNU General Public License along |
|---|
| 32 | * with the GNU C Library; see the file COPYING. If not, write to the Free |
|---|
| 33 | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|---|
| 34 | */ |
|---|
| 35 | |
|---|
| 36 | #ifndef __USAGE_H__ |
|---|
| 37 | #define __USAGE_H__ |
|---|
| 38 | |
|---|
| 39 | #include <sys/time.h> |
|---|
| 40 | #include <sys/resource.h> |
|---|
| 41 | #include <unistd.h> |
|---|
| 42 | #include <iostream> |
|---|
| 43 | |
|---|
| 44 | struct usage_t { |
|---|
| 45 | unsigned int elapsed_time; |
|---|
| 46 | long memory_size; |
|---|
| 47 | usage_t() { |
|---|
| 48 | elapsed_time = 0; |
|---|
| 49 | memory_size = 0; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | void start() { |
|---|
| 53 | struct rusage usage; |
|---|
| 54 | getrusage(RUSAGE_SELF, &usage); |
|---|
| 55 | elapsed_time = usage.ru_utime.tv_sec; |
|---|
| 56 | memory_size = usage.ru_maxrss; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | void cont() { |
|---|
| 60 | struct rusage usage; |
|---|
| 61 | getrusage(RUSAGE_SELF, &usage); |
|---|
| 62 | elapsed_time = usage.ru_utime.tv_sec - elapsed_time; |
|---|
| 63 | memory_size = usage.ru_maxrss; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | void stop() { |
|---|
| 67 | cont(); |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | operator unsigned int () const { |
|---|
| 71 | return elapsed_time; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | long get_memory_size () const { |
|---|
| 75 | return memory_size; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | friend std::ostream & operator << (std::ostream & o, const usage_t & c) { |
|---|
| 79 | return o << c.elapsed_time << "seconds"; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | }; |
|---|
| 83 | |
|---|
| 84 | #endif |
|---|
| 85 | |
|---|
| 86 | /* |
|---|
| 87 | # Local Variables: |
|---|
| 88 | # tab-width: 4; |
|---|
| 89 | # c-basic-offset: 4; |
|---|
| 90 | # c-file-offsets:((innamespace . 0)(inline-open . 0)); |
|---|
| 91 | # indent-tabs-mode: nil; |
|---|
| 92 | # End: |
|---|
| 93 | # |
|---|
| 94 | # vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
|---|
| 95 | */ |
|---|
| 96 | |
|---|