1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
|
// systemtap cache manager
// Copyright (C) 2006-2009 Red Hat Inc.
//
// This file is part of systemtap, and is free software. You can
// redistribute it and/or modify it under the terms of the GNU General
// Public License (GPL); either version 2, or (at your option) any
// later version.
#include "config.h"
#include "session.h"
#include "cache.h"
#include "util.h"
#include "sys/sdt.h"
#include <cerrno>
#include <string>
#include <fstream>
#include <cstring>
#include <cassert>
extern "C" {
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <glob.h>
}
using namespace std;
#define SYSTEMTAP_CACHE_MAX_FILENAME "cache_mb_limit"
#define SYSTEMTAP_CACHE_DEFAULT_MB 64
struct cache_ent_info {
string path;
bool is_module;
size_t size;
long weight; //lower == removed earlier
cache_ent_info(const string& path, bool is_module);
bool operator<(const struct cache_ent_info& other) const
{ return weight < other.weight; }
void unlink() const;
};
void
add_stapconf_to_cache(systemtap_session& s)
{
bool verbose = s.verbose > 1;
string stapconf_src_path = s.tmpdir + "/" + s.stapconf_name;
if (!copy_file(stapconf_src_path, s.stapconf_path, verbose))
{
// NB: this is not so severe as to prevent reuse of the .ko
// already copied.
//
// s.use_script_cache = false;
// return;
}
}
void
add_script_to_cache(systemtap_session& s)
{
bool verbose = s.verbose > 1;
// PR10543: clean the cache *before* we try putting something new into it.
// We don't want to risk having the brand new contents being erased again.
clean_cache(s);
string module_src_path = s.tmpdir + "/" + s.module_name + ".ko";
STAP_PROBE2(stap, cache__add__module, module_src_path.c_str(), s.hash_path.c_str());
if (!copy_file(module_src_path, s.hash_path, verbose))
{
s.use_script_cache = false;
return;
}
// Copy the signature file, if any. It is not an error if this fails.
if (file_exists (module_src_path + ".sgn"))
copy_file(module_src_path + ".sgn", s.hash_path + ".sgn", verbose);
string c_dest_path = s.hash_path;
if (endswith(c_dest_path, ".ko"))
c_dest_path.resize(c_dest_path.size() - 3);
c_dest_path += ".c";
STAP_PROBE2(stap, cache__add__source, s.translated_source.c_str(), c_dest_path.c_str());
if (!copy_file(s.translated_source, c_dest_path, verbose))
{
// NB: this is not so severe as to prevent reuse of the .ko
// already copied.
//
// s.use_script_cache = false;
}
}
bool
get_stapconf_from_cache(systemtap_session& s)
{
if (s.poison_cache)
return false;
string stapconf_dest_path = s.tmpdir + "/" + s.stapconf_name;
int fd_stapconf;
// See if stapconf exists
fd_stapconf = open(s.stapconf_path.c_str(), O_RDONLY);
if (fd_stapconf == -1)
{
// It isn't in cache.
return false;
}
// Copy the stapconf header file to the destination
if (!copy_file(s.stapconf_path, stapconf_dest_path))
{
close(fd_stapconf);
return false;
}
// We're done with this file handle.
close(fd_stapconf);
if (s.verbose > 1)
clog << "Pass 4: using cached " << s.stapconf_path << endl;
return true;
}
bool
get_script_from_cache(systemtap_session& s)
{
if (s.poison_cache)
return false;
string module_dest_path = s.tmpdir + "/" + s.module_name + ".ko";
string c_src_path = s.hash_path;
int fd_module, fd_c;
if (endswith(c_src_path, ".ko"))
c_src_path.resize(c_src_path.size() - 3);
c_src_path += ".c";
// See if module exists
fd_module = open(s.hash_path.c_str(), O_RDONLY);
if (fd_module == -1)
{
// It isn't in cache.
return false;
}
// See if C file exists.
fd_c = open(c_src_path.c_str(), O_RDONLY);
if (fd_c == -1)
{
// The module is there, but the C file isn't. Cleanup and
// return.
close(fd_module);
unlink(s.hash_path.c_str());
return false;
}
// Copy the cached C file to the destination
if (!copy_file(c_src_path, s.translated_source))
{
close(fd_module);
close(fd_c);
return false;
}
// Copy the cached module to the destination (if needed)
if (s.last_pass != 3)
{
if (!copy_file(s.hash_path, module_dest_path))
{
unlink(c_src_path.c_str());
close(fd_module);
close(fd_c);
return false;
}
// Copy the module signature file, if any.
// It is not an error if this fails.
if (file_exists (s.hash_path + ".sgn"))
copy_file(s.hash_path + ".sgn", module_dest_path + ".sgn");
}
// We're done with these file handles.
close(fd_module);
close(fd_c);
// To preserve semantics (since this will happen if we're not
// caching), display the C source if the last pass is 3.
if (s.last_pass == 3)
{
ifstream i (s.translated_source.c_str());
cout << i.rdbuf();
}
// And similarly, display probe module name for -p4.
if (s.last_pass == 4)
cout << s.hash_path << endl;
// If everything worked, tell the user. We need to do this here,
// since if copying the cached C file works, but copying the cached
// module fails, we remove the cached C file and let the C file get
// regenerated.
if (s.verbose)
{
clog << "Pass 3: using cached " << c_src_path << endl;
if (s.last_pass != 3)
clog << "Pass 4: using cached " << s.hash_path << endl;
}
STAP_PROBE2(stap, cache__get, c_src_path.c_str(), s.hash_path.c_str());
return true;
}
void
clean_cache(systemtap_session& s)
{
if (s.cache_path != "")
{
/* Get cache size limit from file in the stap cache dir */
string cache_max_filename = s.cache_path + "/";
cache_max_filename += SYSTEMTAP_CACHE_MAX_FILENAME;
ifstream cache_max_file(cache_max_filename.c_str(), ios::in);
unsigned long cache_mb_max;
if (cache_max_file.is_open())
{
cache_max_file >> cache_mb_max;
cache_max_file.close();
}
else
{
//file doesnt exist, create a default size
ofstream default_cache_max(cache_max_filename.c_str(), ios::out);
default_cache_max << SYSTEMTAP_CACHE_DEFAULT_MB << endl;
cache_mb_max = SYSTEMTAP_CACHE_DEFAULT_MB;
if (s.verbose > 1)
clog << "Cache limit file " << s.cache_path << "/"
<< SYSTEMTAP_CACHE_MAX_FILENAME
<< " missing, creating default." << endl;
}
//glob for all kernel modules in the cache dir
glob_t cache_glob;
string glob_str = s.cache_path + "/*/*.ko";
glob(glob_str.c_str(), 0, NULL, &cache_glob);
set<struct cache_ent_info> cache_contents;
unsigned long cache_size_b = 0;
//grab info for each cache entry (.ko and .c)
for (unsigned int i = 0; i < cache_glob.gl_pathc; i++)
{
string cache_ent_path = cache_glob.gl_pathv[i];
cache_ent_path.resize(cache_ent_path.length() - 3);
struct cache_ent_info cur_info(cache_ent_path, true);
if (cur_info.size != 0 && cur_info.weight != 0)
{
cache_size_b += cur_info.size;
cache_contents.insert(cur_info);
}
}
globfree(&cache_glob);
//grab info for each typequery user module (.so)
glob_str = s.cache_path + "/*/*.so";
glob(glob_str.c_str(), 0, NULL, &cache_glob);
for (unsigned int i = 0; i < cache_glob.gl_pathc; i++)
{
string cache_ent_path = cache_glob.gl_pathv[i];
struct cache_ent_info cur_info(cache_ent_path, false);
if (cur_info.size != 0 && cur_info.weight != 0)
{
cache_size_b += cur_info.size;
cache_contents.insert(cur_info);
}
}
globfree(&cache_glob);
//grab info for each stapconf cache entry (.h)
glob_str = s.cache_path + "/*/*.h";
glob(glob_str.c_str(), 0, NULL, &cache_glob);
for (unsigned int i = 0; i < cache_glob.gl_pathc; i++)
{
string cache_ent_path = cache_glob.gl_pathv[i];
struct cache_ent_info cur_info(cache_ent_path, false);
if (cur_info.size != 0 && cur_info.weight != 0)
{
cache_size_b += cur_info.size;
cache_contents.insert(cur_info);
}
}
globfree(&cache_glob);
// grab info for each staphash log file (.log)
glob_str = s.cache_path + "/*/*.log";
glob(glob_str.c_str(), 0, NULL, &cache_glob);
for (unsigned int i = 0; i < cache_glob.gl_pathc; i++)
{
string cache_ent_path = cache_glob.gl_pathv[i];
struct cache_ent_info cur_info(cache_ent_path, false);
if (cur_info.size != 0 && cur_info.weight != 0)
{
cache_size_b += cur_info.size;
cache_contents.insert(cur_info);
}
}
globfree(&cache_glob);
set<struct cache_ent_info>::iterator i;
unsigned long r_cache_size = cache_size_b;
string removed_dirs = "";
//unlink .ko and .c until the cache size is under the limit
for (i = cache_contents.begin(); i != cache_contents.end(); ++i)
{
if ( (r_cache_size / 1024 / 1024) < cache_mb_max) //convert r_cache_size to MiB
break;
STAP_PROBE1(stap, cache__clean, (i->path).c_str());
//remove this (*i) cache_entry, add to removed list
i->unlink();
r_cache_size -= i->size;
removed_dirs += i->path + ", ";
}
cache_contents.clear();
if (s.verbose > 1 && removed_dirs != "")
{
//remove trailing ", "
removed_dirs = removed_dirs.substr(0, removed_dirs.length() - 2);
clog << "Cache cleaning successful, removed entries: "
<< removed_dirs << endl;
}
}
else
{
if (s.verbose > 1)
clog << "Cache cleaning skipped, no cache path." << endl;
}
}
//Assign a weight for a particular file. A lower weight
// will be removed before a higher weight.
//TODO: for now use system mtime... later base a
// weighting on size, ctime, atime etc..
static long
get_file_weight(const string &path)
{
time_t dir_mtime = 0;
struct stat dir_stat_info;
if (stat(path.c_str(), &dir_stat_info) == 0)
//GNU struct stat defines st_atime as st_atim.tv_sec
// but it doesnt seem to work properly in practice
// so use st_atim.tv_sec -- bad for portability?
dir_mtime = dir_stat_info.st_mtim.tv_sec;
return dir_mtime;
}
cache_ent_info::cache_ent_info(const string& path, bool is_module):
path(path), is_module(is_module)
{
if (is_module)
{
string mod_path = path + ".ko";
string modsgn_path = path + ".ko.sgn";
string source_path = path + ".c";
string hash_path = path + ".log";
size = get_file_size(mod_path)
+ get_file_size(modsgn_path);
+ get_file_size(source_path);
+ get_file_size(hash_path);
weight = get_file_weight(mod_path);
}
else
{
size = get_file_size(path);
weight = get_file_weight(path);
}
}
void
cache_ent_info::unlink() const
{
if (is_module)
{
string mod_path = path + ".ko";
string modsgn_path = path + ".ko.sgn";
string source_path = path + ".c";
string hash_path = path + ".log";
::unlink(mod_path.c_str());
::unlink(modsgn_path.c_str());
::unlink(source_path.c_str());
::unlink(hash_path.c_str());
}
else
::unlink(path.c_str());
}
/* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
|