Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Zoltan2_Util.cpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // Zoltan2: A package of combinatorial algorithms for scientific computing
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Karen Devine (kddevin@sandia.gov)
39 // Erik Boman (egboman@sandia.gov)
40 // Siva Rajamanickam (srajama@sandia.gov)
41 //
42 // ***********************************************************************
43 //
44 // @HEADER
45 
50 #include <Zoltan2_Util.hpp>
51 #include <Teuchos_OpaqueWrapper.hpp>
52 
53 #include <iostream>
54 #include <sstream>
55 #include <fstream>
56 #include <string>
57 
58 #ifndef _MSC_VER
59 #include <unistd.h>
60 #endif
61 
62 namespace Zoltan2{
63 
64 #ifndef _WIN32
65 /* On a linux node, find the total memory currently allocated
66  * to this process.
67  * Return the number of kilobytes allocated to this process.
68  * Return 0 if it is not possible to determine this.
69  */
71 {
72 long pageSize;
73 
74 #ifdef _SC_PAGESIZE
75  pageSize = sysconf(_SC_PAGESIZE);
76 #else
77 #warning "Page size query is not possible. No per-process memory stats."
78  return 0;
79 #endif
80 
81  pid_t pid = getpid();
82  std::ostringstream fname;
83  fname << "/proc/" << pid << "/statm";
84  std::ifstream memFile;
85 
86  try{
87  memFile.open(fname.str().c_str());
88  }
89  catch (...){
90  return 0;
91  }
92 
93  char buf[128];
94  memset(buf, 0, 128);
95  while (memFile.good()){
96  memFile.getline(buf, 128);
97  break;
98  }
99 
100  memFile.close();
101 
102  std::istringstream sbuf(buf);
103  long totalPages;
104  sbuf >> totalPages;
105 
106  long pageKBytes = pageSize / 1024;
107  totalPages = atol(buf);
108 
109  return totalPages * pageKBytes;
110 }
111 #else
112 long getProcessKilobytes()
113 {
114 #pragma message ("Zoltan2_Util.cpp: Page size query is not implemented on windows. No per-process memory stats.")
115  return 0;
116 }
117 #endif
118 
119 } // namespace Zoltan2
long getProcessKilobytes()
A gathering of useful namespace methods.