Web Wispers 1.2.2 C++ Unit Test Coverage |
|
|
|
|
Branch data Line data Source code
1 : : /*
2 : : * Copyright 2009 Savarese Software Research Corporation
3 : : *
4 : : * Licensed under the Apache License, Version 2.0 (the "License");
5 : : * you may not use this file except in compliance with the License.
6 : : * You may obtain a copy of the License at
7 : : *
8 : : * https://www.savarese.com/software/ApacheLicense-2.0
9 : : *
10 : : * Unless required by applicable law or agreed to in writing, software
11 : : * distributed under the License is distributed on an "AS IS" BASIS,
12 : : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 : : * See the License for the specific language governing permissions and
14 : : * limitations under the License.
15 : : */
16 : :
17 : : #include <ssrc/wispers/utility/memusage.h>
18 : :
19 : : // TODO: Implement for FreeBSD.
20 : : // TODO: exceptions? errors? scale all values to bytes?
21 : : #if defined(WISPERS_HAVE_GET_MEMUSAGE)
22 : :
23 : : #include <sstream>
24 : :
25 : : # if defined(linux) || defined(__linux) || defined(__linux__)
26 : :
27 : : # include <fstream>
28 : :
29 : : __BEGIN_NS_SSRC_WSPR_UTILITY
30 : :
31 : : bool get_memusage(const unsigned int pid, memusage & usage) {
32 : : std::ostringstream buf;
33 : :
34 : : buf << "/proc/" << pid << "/statm";
35 : :
36 : : std::ifstream in(buf.str().c_str());
37 : :
38 : : if(in) {
39 : : in >> usage.size >> usage.resident >> usage.share
40 : : >> usage.text >> usage.lib >> usage.data >> usage.dt;
41 : :
42 : : usage.page_size = ::sysconf(_SC_PAGE_SIZE);
43 : :
44 : : return true;
45 : : }
46 : :
47 : : return false;
48 : : }
49 : :
50 : : __END_NS_SSRC_WSPR_UTILITY
51 : :
52 : : # elif defined(sun) || defined(__sun) || defined(__sun__)
53 : :
54 : : # include <fcntl.h>
55 : : # include <procfs.h>
56 : :
57 : : __BEGIN_NS_SSRC_WSPR_UTILITY
58 : :
59 : 1 : bool get_memusage(const unsigned int pid, memusage & usage) {
60 : : ::psinfo_t psinfo;
61 : : ::pstatus_t pstatus;
62 : 2 : std::ostringstream buf;
63 : :
64 [ + - + - : 1 : buf << "/proc/" << pid << "/status";
+ - ]
65 : :
66 [ + - + - : 1 : int fd = ::open(buf.str().c_str(), O_RDONLY);
+ - + - ]
67 : :
68 [ - + ]: 1 : if(fd < 0)
69 : 0 : return false;
70 : :
71 [ + - - + ]: 1 : if(::read(fd, &pstatus, sizeof(pstatus_t)) != sizeof(pstatus_t))
72 : 0 : return false;
73 : :
74 [ + - ]: 1 : ::close(fd);
75 : :
76 [ + - + - : 1 : buf.str("");
+ - ]
77 [ + - + - : 1 : buf << "/proc/" << pid << "/psinfo";
+ - ]
78 : :
79 [ + - + - : 1 : fd = ::open(buf.str().c_str(), O_RDONLY);
+ - + - ]
80 : :
81 [ - + ]: 1 : if(fd < 0)
82 : 0 : return false;
83 : :
84 [ + - - + ]: 1 : if(::read(fd, &psinfo, sizeof(psinfo_t)) != sizeof(psinfo_t))
85 : 0 : return false;
86 : :
87 [ + - ]: 1 : ::close(fd);
88 : :
89 : 1 : usage.size = psinfo.pr_size;
90 : 1 : usage.resident = psinfo.pr_rssize;
91 : 1 : usage.heap = pstatus.pr_brksize;
92 : 1 : usage.stack = pstatus.pr_stksize;
93 : :
94 : 1 : return true;
95 : : }
96 : :
97 : : __END_NS_SSRC_WSPR_UTILITY
98 : :
99 : : # endif
100 : :
101 : : #else
102 : :
103 : : __BEGIN_NS_SSRC_WSPR_UTILITY
104 : :
105 : : // Dummy function to allow module to compile.
106 : : bool get_memusage(const unsigned int pid) { return false; }
107 : :
108 : : __END_NS_SSRC_WSPR_UTILITY
109 : :
110 : : #endif
|
Copyright © 2017 Savarese Software Research Corporation. All rights reserved