Zoltan2
Loading...
Searching...
No Matches
Zoltan2_Util.cpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// Zoltan2: A package of combinatorial algorithms for scientific computing
4//
5// Copyright 2012 NTESS and the Zoltan2 contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
14#include <Zoltan2_Util.hpp>
15#include <Teuchos_OpaqueWrapper.hpp>
16
17#include <iostream>
18#include <sstream>
19#include <fstream>
20#include <string>
21
22#ifndef _MSC_VER
23#include <unistd.h>
24#endif
25
26namespace Zoltan2{
27
28#ifndef _WIN32
29/* On a linux node, find the total memory currently allocated
30 * to this process.
31 * Return the number of kilobytes allocated to this process.
32 * Return 0 if it is not possible to determine this.
33 */
35{
36long pageSize;
37
38#ifdef _SC_PAGESIZE
39 pageSize = sysconf(_SC_PAGESIZE);
40#else
41#warning "Page size query is not possible. No per-process memory stats."
42 return 0;
43#endif
44
45 pid_t pid = getpid();
46 std::ostringstream fname;
47 fname << "/proc/" << pid << "/statm";
48 std::ifstream memFile;
49
50 try{
51 memFile.open(fname.str().c_str());
52 }
53 catch (...){
54 return 0;
55 }
56
57 char buf[128];
58 memset(buf, 0, 128);
59 while (memFile.good()){
60 memFile.getline(buf, 128);
61 break;
62 }
63
64 memFile.close();
65
66 std::istringstream sbuf(buf);
67 long totalPages;
68 sbuf >> totalPages;
69
70 long pageKBytes = pageSize / 1024;
71 totalPages = atol(buf);
72
73 return totalPages * pageKBytes;
74}
75#else
77{
78#pragma message ("Zoltan2_Util.cpp: Page size query is not implemented on windows. No per-process memory stats.")
79 return 0;
80}
81#endif
82
83} // namespace Zoltan2
A gathering of useful namespace methods.
Created by mbenlioglu on Aug 31, 2020.
long getProcessKilobytes()