cppthread 1.1.16
C++ Thread Library
deswappify.cpp
Go to the documentation of this file.
1// Copyright (c) 2026 Made to Order Software Corp. All Rights Reserved
2//
3// https://snapwebsites.org/project/cppthread
4// contact@m2osw.com
5//
6// This program is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation; either version 2 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License along
17// with this program; if not, write to the Free Software Foundation, Inc.,
18// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
33// self
34//
35#include "cppthread/thread.h"
36
37
38
39// snapdev
40//
41#include <snapdev/glob_to_list.h>
42#include <snapdev/hexadecimal_string.h>
43
44
45// C++
46//
47#include <fstream>
48#include <sstream>
49
50
51// last include
52//
53#include <snapdev/poison.h>
54
55
56
57
58namespace cppthread
59{
60
61
62
63
64
83int deswappify(pid_t pid)
84{
85 std::stringstream smaps;
86 smaps << "/proc/" << pid << "/smaps";
87
88 std::stringstream mem;
89 mem << "/proc/" << pid << "/mem";
90
91 try
92 {
93 std::ifstream in(smaps.str());
94 std::ifstream mb(mem.str());
95
96 std::int64_t start(0);
97 std::int64_t end(0);
98 while(in)
99 {
100 std::string l;
101 std::getline(in, l);
102 if(l.empty())
103 {
104 break;
105 }
106
107 // each block is separated by one line with two hexadecimal
108 // numbers (addresses) separated by a dash:
109 //
110 // start-end <flags> ...
111 //
112 // if not valid, then try the next possibility which is
113 // a line starting with "Swap:"
114 //
115 char const * s(l.c_str());
116
117 std::int64_t new_start(0);
118 for(; snapdev::is_hexdigit(*s); ++s)
119 {
120 new_start *= 16;
121 new_start += snapdev::hexdigit_to_number(*s);
122 }
123 if(*s == '-')
124 {
125 // doing good so far...
126 //
127 end = 0;
128 for(++s; snapdev::is_hexdigit(*s); ++s)
129 {
130 end *= 16;
131 end += snapdev::hexdigit_to_number(*s);
132 }
133 if(new_start != 0
134 && end != 0)
135 {
136 // assume start-end ... found
137 //
138 start = new_start;
139 continue;
140 }
141 else
142 {
143 end = 0;
144 }
145 }
146 if(start == 0
147 || end == 0)
148 {
149 continue;
150 }
151
152 if(*s == 'S'
153 && s[1] == 'w'
154 && s[2] == 'a'
155 && s[3] == 'p'
156 && s[4] == ':')
157 {
158 s += 6;
159 while(isspace(*s))
160 {
161 ++s;
162 }
163 int kb(0);
164 for(; *s >= '0' && *s <= '9'; ++s)
165 {
166 kb *= 10;
167 kb += *s - '0';
168 }
169 while(isspace(*s))
170 {
171 ++s;
172 }
173 if(kb > 0
174 && s[0] == 'k'
175 && s[1] == 'B')
176 {
177 // found a swapped out memory block, read it
178 //
179 try
180 {
181 mb.seekg(start);
182 char buf[4096]; // TODO: verify page size!
183 for(; start < end; start += 4096)
184 {
185 mb.read(buf, 4096);
186 }
187 start = 0;
188 end = 0;
189 }
190 catch(std::system_error const & e)
191 {
192 return e.code().value();
193 }
194 }
195 // else -- not a valid Swap: ... entry or not swapped out (0 kB)
196 }
197 // else -- ignore the other fields
198 }
199 }
200 catch(std::system_error const & e)
201 {
202 return e.code().value();
203 }
204
205 return 0;
206}
207
208
209
210} // namespace cppthread
211// vim: ts=4 sw=4 et
int deswappify(pid_t pid)
Go through the swapped out sections of a process and deswappify them.
logger & end(logger &l)
Close a log statement.
Definition log.h:90
Thread Runner and Managers.

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.