| 1 |
// Process memory query and utility functions. |
| 2 |
// Copyright (C) 2009-2017 Red Hat Inc. |
| 3 |
// |
| 4 |
// This file is part of systemtap, and is free software. You can |
| 5 |
// redistribute it and/or modify it under the terms of the GNU General |
| 6 |
// Public License (GPL); either version 2, or (at your option) any |
| 7 |
// later version. |
| 8 |
|
| 9 |
// <tapsetdescription> |
| 10 |
// Process memory query and utility functions provide information about |
| 11 |
// the memory usage of the current application. These functions provide |
| 12 |
// information about the full size, resident, shared, code and data used |
| 13 |
// by the current process. And provide utility functions to query the |
| 14 |
// page size of the current architecture and create human readable string |
| 15 |
// representations of bytes and pages used. |
| 16 |
// </tapsetdescription> |
| 17 |
|
| 18 |
%{ |
| 19 |
/* PF_BORROWED_MM got renamed to PF_KTHREAD with same semantics somewhere. */ |
| 20 |
#ifdef PF_BORROWED_MM |
| 21 |
#define _STP_PF_KTHREAD PF_BORROWED_MM |
| 22 |
#elif defined(PF_KTHREAD) |
| 23 |
#define _STP_PF_KTHREAD PF_KTHREAD |
| 24 |
#else |
| 25 |
/* 2.6.9 doesn't have either PF_BORROWED_MM or PF_KTHREAD */ |
| 26 |
#define _STP_PF_KTHREAD 0 |
| 27 |
#endif |
| 28 |
|
| 29 |
/* PF_STARTING was removed in later kernels */ |
| 30 |
#ifdef PF_STARTING |
| 31 |
#define _STP_BAD_TASK_FLAGS (_STP_PF_KTHREAD | PF_EXITING | PF_STARTING) |
| 32 |
#else |
| 33 |
#define _STP_BAD_TASK_FLAGS (_STP_PF_KTHREAD | PF_EXITING) |
| 34 |
#endif |
| 35 |
|
| 36 |
|
| 37 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,34) |
| 38 |
#include <linux/mm_types.h> |
| 39 |
#else |
| 40 |
/* Define our own mm types */ |
| 41 |
enum { |
| 42 |
MM_FILEPAGES, |
| 43 |
MM_ANONPAGES |
| 44 |
}; |
| 45 |
#endif |
| 46 |
%} |
| 47 |
|
| 48 |
/* Try to be slightly paranoid. Only returns 1 if the task isn't |
| 49 |
starting, exiting or (coopted by) a kernel thread. */ |
| 50 |
@__private30 function _stp_valid_task:long(tsk:long) |
| 51 |
%{ |
| 52 |
struct task_struct *tsk = (struct task_struct *)(uintptr_t)STAP_ARG_tsk; |
| 53 |
|
| 54 |
STAP_RETVALUE = 0; |
| 55 |
if (tsk) { |
| 56 |
unsigned int flags = kread(&(tsk->flags)); |
| 57 |
|
| 58 |
if (flags & ~_STP_BAD_TASK_FLAGS) |
| 59 |
STAP_RETVALUE = 1; |
| 60 |
} |
| 61 |
CATCH_DEREF_FAULT(); |
| 62 |
%} |
| 63 |
|
| 64 |
@__private30 function _MM_FILEPAGES:long() |
| 65 |
{ |
| 66 |
return @const("MM_FILEPAGES") |
| 67 |
} |
| 68 |
|
| 69 |
@__private30 function _MM_ANONPAGES:long() |
| 70 |
{ |
| 71 |
return @const("MM_ANONPAGES") |
| 72 |
} |
| 73 |
|
| 74 |
%(systemtap_v > "4.2" %? |
| 75 |
@__private30 function _MM_SHMEMPAGES:long() |
| 76 |
%{ /* pure */ |
| 77 |
STAP_RETVALUE = |
| 78 |
#ifdef STAPCONF_MM_SHMEMPAGES |
| 79 |
MM_SHMEMPAGES; |
| 80 |
#else |
| 81 |
0; |
| 82 |
#endif |
| 83 |
%} |
| 84 |
%) |
| 85 |
|
| 86 |
@__private30 function _stp_get_mm_counter:long(mm_ptr:long, member:long) |
| 87 |
{ |
| 88 |
mm = & @mm(mm_ptr) |
| 89 |
/* kernels >= 2.6.34, 3.0 (2.6.40 fedora) and later always use atomics */ |
| 90 |
if (@defined(&@mm(0)->rss_stat)) { |
| 91 |
%( kernel_v >= "2.6.40" || CONFIG_NR_CPUS >= CONFIG_SPLIT_PTLOCK_CPUS %? |
| 92 |
val = atomic_long_read(&mm->rss_stat->count[member]) |
| 93 |
%: |
| 94 |
val = mm->rss_stat->count[member] |
| 95 |
%) |
| 96 |
if (val < 0) |
| 97 |
return 0 |
| 98 |
} |
| 99 |
/* kernels < 2.6.34 && kernels >= 2.6.12 */ |
| 100 |
else if (@defined(&@mm(0)->_file_rss)) { |
| 101 |
if (member == _MM_FILEPAGES()) { |
| 102 |
%( CONFIG_NR_CPUS >= CONFIG_SPLIT_PTLOCK_CPUS %? |
| 103 |
val = atomic_long_read(&mm->_file_rss) |
| 104 |
%: |
| 105 |
val = mm->_file_rss |
| 106 |
%) |
| 107 |
} |
| 108 |
else if (member == _MM_ANONPAGES()) { |
| 109 |
%( CONFIG_NR_CPUS >= CONFIG_SPLIT_PTLOCK_CPUS %? |
| 110 |
val = atomic_long_read(&mm->_anon_rss) |
| 111 |
%: |
| 112 |
val = mm->_anon_rss |
| 113 |
%) |
| 114 |
} |
| 115 |
} |
| 116 |
/* kernels < 2.6.12 */ |
| 117 |
else { |
| 118 |
if (member == _MM_FILEPAGES()) |
| 119 |
val = mm->rss |
| 120 |
else if (member == _MM_ANONPAGES()) |
| 121 |
val = mm->anon_rss |
| 122 |
} |
| 123 |
return val |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* sfunction proc_mem_size - Total program virtual memory size in pages |
| 128 |
* |
| 129 |
* Description: Returns the total virtual memory size in pages of the |
| 130 |
* current process, or zero when there is no current process or the |
| 131 |
* number of pages couldn't be retrieved. |
| 132 |
*/ |
| 133 |
function proc_mem_size:long () |
| 134 |
{ |
| 135 |
task = task_current() |
| 136 |
if (_stp_valid_task(task)) { |
| 137 |
mm = @task(task)->mm |
| 138 |
if (mm != 0) |
| 139 |
return @mm(mm)->total_vm |
| 140 |
} |
| 141 |
return 0 |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* sfunction proc_mem_size - Total program virtual memory size in pages |
| 146 |
* |
| 147 |
* @pid: The pid of process to examine |
| 148 |
* |
| 149 |
* Description: Returns the total virtual memory size in pages of the |
| 150 |
* given process, or zero when that process doesn't exist or the |
| 151 |
* number of pages couldn't be retrieved. |
| 152 |
*/ |
| 153 |
function proc_mem_size:long (pid:long) |
| 154 |
{ |
| 155 |
task = pid2task(pid) |
| 156 |
if (_stp_valid_task(task)) { |
| 157 |
mm = @task(task)->mm |
| 158 |
if (mm != 0) |
| 159 |
return @mm(mm)->total_vm |
| 160 |
} |
| 161 |
return 0 |
| 162 |
} |
| 163 |
function proc_mem_size_pid:long (pid:long) |
| 164 |
{ |
| 165 |
return proc_mem_size(pid) |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* sfunction proc_mem_rss - Program resident set size in pages |
| 170 |
* |
| 171 |
* Description: Returns the resident set size in pages of the current |
| 172 |
* process, or zero when there is no current process or the number of |
| 173 |
* pages couldn't be retrieved. |
| 174 |
*/ |
| 175 |
function proc_mem_rss:long () |
| 176 |
{ |
| 177 |
task = task_current() |
| 178 |
if (_stp_valid_task(task)) { |
| 179 |
mm = @task(task)->mm |
| 180 |
if (mm != 0) |
| 181 |
return (_stp_get_mm_counter(mm, _MM_FILEPAGES()) |
| 182 |
+ _stp_get_mm_counter(mm, _MM_ANONPAGES()) |
| 183 |
%(systemtap_v > "4.2" %? |
| 184 |
+ _stp_get_mm_counter(mm, _MM_SHMEMPAGES()) |
| 185 |
%) |
| 186 |
) |
| 187 |
} |
| 188 |
return 0 |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* sfunction proc_mem_rss - Program resident set size in pages |
| 193 |
* |
| 194 |
* @pid: The pid of process to examine |
| 195 |
* |
| 196 |
* Description: Returns the resident set size in pages of the given |
| 197 |
* process, or zero when the process doesn't exist or the number of |
| 198 |
* pages couldn't be retrieved. |
| 199 |
*/ |
| 200 |
function proc_mem_rss:long (pid:long) |
| 201 |
{ |
| 202 |
task = pid2task(pid) |
| 203 |
if (_stp_valid_task(task)) { |
| 204 |
mm = @task(task)->mm |
| 205 |
if (mm != 0) |
| 206 |
return (_stp_get_mm_counter(mm, _MM_FILEPAGES()) |
| 207 |
+ _stp_get_mm_counter(mm, _MM_ANONPAGES()) |
| 208 |
%(systemtap_v > "4.2" %? |
| 209 |
+ _stp_get_mm_counter(mm, _MM_SHMEMPAGES()) |
| 210 |
%) |
| 211 |
) |
| 212 |
} |
| 213 |
return 0 |
| 214 |
} |
| 215 |
function proc_mem_rss_pid:long (pid:long) |
| 216 |
{ |
| 217 |
return proc_mem_rss(pid) |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* sfunction proc_mem_shr - Program shared pages (from shared mappings) |
| 222 |
* |
| 223 |
* Description: Returns the shared pages (from shared mappings) of the |
| 224 |
* current process, or zero when there is no current process or the |
| 225 |
* number of pages couldn't be retrieved. |
| 226 |
*/ |
| 227 |
function proc_mem_shr:long () |
| 228 |
{ |
| 229 |
task = task_current() |
| 230 |
if (_stp_valid_task(task)) { |
| 231 |
mm = @task(task)->mm |
| 232 |
if (mm != 0) |
| 233 |
return _stp_get_mm_counter(mm, _MM_FILEPAGES()) |
| 234 |
} |
| 235 |
return 0 |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* sfunction proc_mem_shr - Program shared pages (from shared mappings) |
| 240 |
* |
| 241 |
* @pid: The pid of process to examine |
| 242 |
* |
| 243 |
* Description: Returns the shared pages (from shared mappings) of the |
| 244 |
* given process, or zero when the process doesn't exist or the |
| 245 |
* number of pages couldn't be retrieved. |
| 246 |
*/ |
| 247 |
function proc_mem_shr:long (pid:long) |
| 248 |
{ |
| 249 |
task = pid2task(pid) |
| 250 |
if (_stp_valid_task(task)) { |
| 251 |
mm = @task(task)->mm |
| 252 |
if (mm != 0) |
| 253 |
return _stp_get_mm_counter(mm, _MM_FILEPAGES()) |
| 254 |
} |
| 255 |
return 0 |
| 256 |
} |
| 257 |
function proc_mem_shr_pid:long (pid:long) |
| 258 |
{ |
| 259 |
return proc_mem_shr(pid) |
| 260 |
} |
| 261 |
|
| 262 |
@__private30 function _stp_mem_txt_adjust:long (start_code:long, end_code:long) |
| 263 |
%{ /* pure */ |
| 264 |
unsigned long start_code = (unsigned long) STAP_ARG_start_code; |
| 265 |
unsigned long end_code = (unsigned long) STAP_ARG_end_code; |
| 266 |
STAP_RETVALUE = (PAGE_ALIGN(end_code) |
| 267 |
- (start_code & PAGE_MASK)) >> PAGE_SHIFT; |
| 268 |
%} |
| 269 |
|
| 270 |
/** |
| 271 |
* sfunction proc_mem_txt - Program text (code) size in pages |
| 272 |
* |
| 273 |
* Description: Returns the current process text (code) size in pages, |
| 274 |
* or zero when there is no current process or the number of pages |
| 275 |
* couldn't be retrieved. |
| 276 |
*/ |
| 277 |
function proc_mem_txt:long () |
| 278 |
{ |
| 279 |
task = task_current() |
| 280 |
if (_stp_valid_task(task)) { |
| 281 |
mm = @task(task)->mm |
| 282 |
if (mm != 0) { |
| 283 |
s = @mm(mm)->start_code |
| 284 |
e = @mm(mm)->end_code |
| 285 |
return _stp_mem_txt_adjust(s, e) |
| 286 |
} |
| 287 |
} |
| 288 |
return 0 |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* sfunction proc_mem_txt - Program text (code) size in pages |
| 293 |
* |
| 294 |
* @pid: The pid of process to examine |
| 295 |
* |
| 296 |
* Description: Returns the given process text (code) size in pages, |
| 297 |
* or zero when the process doesn't exist or the number of pages |
| 298 |
* couldn't be retrieved. |
| 299 |
*/ |
| 300 |
function proc_mem_txt:long (pid:long) |
| 301 |
{ |
| 302 |
task = pid2task(pid) |
| 303 |
if (_stp_valid_task(task)) { |
| 304 |
mm = @task(task)->mm |
| 305 |
if (mm != 0) { |
| 306 |
s = @mm(mm)->start_code |
| 307 |
e = @mm(mm)->end_code |
| 308 |
return _stp_mem_txt_adjust (s, e) |
| 309 |
} |
| 310 |
} |
| 311 |
return 0 |
| 312 |
} |
| 313 |
function proc_mem_txt_pid:long (pid:long) |
| 314 |
{ |
| 315 |
return proc_mem_txt(pid) |
| 316 |
} |
| 317 |
|
| 318 |
/** |
| 319 |
* sfunction proc_mem_data - Program data size (data + stack) in pages |
| 320 |
* |
| 321 |
* Description: Returns the current process data size (data + stack) |
| 322 |
* in pages, or zero when there is no current process or the number of |
| 323 |
* pages couldn't be retrieved. |
| 324 |
*/ |
| 325 |
function proc_mem_data:long () |
| 326 |
{ |
| 327 |
task = task_current() |
| 328 |
if (_stp_valid_task(task)) { |
| 329 |
mm = @task(task)->mm |
| 330 |
if (mm != 0) { |
| 331 |
return (@defined(&@mm(0)->data_vm) |
| 332 |
? (@mm(mm)->data_vm + @mm(mm)->stack_vm) |
| 333 |
: (@mm(mm)->total_vm - @mm(mm)->shared_vm)) |
| 334 |
} |
| 335 |
} |
| 336 |
return 0 |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* sfunction proc_mem_data - Program data size (data + stack) in pages |
| 341 |
* |
| 342 |
* @pid: The pid of process to examine |
| 343 |
* |
| 344 |
* Description: Returns the given process data size (data + stack) |
| 345 |
* in pages, or zero when the process doesn't exist or the number of |
| 346 |
* pages couldn't be retrieved. |
| 347 |
*/ |
| 348 |
function proc_mem_data:long (pid:long) |
| 349 |
{ |
| 350 |
task = pid2task(pid) |
| 351 |
if (_stp_valid_task(task)) { |
| 352 |
mm = @task(task)->mm |
| 353 |
if (mm != 0) { |
| 354 |
return (@defined(&@mm(0)->data_vm) |
| 355 |
? (@mm(mm)->data_vm + @mm(mm)->stack_vm) |
| 356 |
: (@mm(mm)->total_vm - @mm(mm)->shared_vm)) |
| 357 |
} |
| 358 |
} |
| 359 |
return 0 |
| 360 |
} |
| 361 |
function proc_mem_data_pid:long (pid:long) |
| 362 |
{ |
| 363 |
return proc_mem_data(pid) |
| 364 |
} |
| 365 |
|
| 366 |
/** |
| 367 |
* sfunction mem_page_size - Number of bytes in a page for this architecture |
| 368 |
*/ |
| 369 |
function mem_page_size:long () |
| 370 |
{ |
| 371 |
return @const("PAGE_SIZE") |
| 372 |
} |
| 373 |
|
| 374 |
// Return a 5 character wide string " x.yyp", " xx.yp", " xxxp", "xxxxp". |
| 375 |
@__private30 function _stp_number_to_string_postfix:string (x:long, y:long, p:string) |
| 376 |
{ |
| 377 |
if (x < 10) |
| 378 |
return sprintf("%d.%.2d%s", x, y * 100 / 1024, p); |
| 379 |
if (x < 100) |
| 380 |
return sprintf("%2d.%d%s", x, y * 10 / 1024, p); |
| 381 |
return sprintf("%4d%s", x, p); |
| 382 |
} |
| 383 |
|
| 384 |
/** |
| 385 |
* sfunction bytes_to_string - Human readable string for given bytes |
| 386 |
* |
| 387 |
* @bytes: Number of bytes to translate. |
| 388 |
* |
| 389 |
* Description: Returns a string representing the number of bytes (up |
| 390 |
* to 1024 bytes), the number of kilobytes (when less than 1024K) |
| 391 |
* postfixed by 'K', the number of megabytes (when less than 1024M) |
| 392 |
* postfixed by 'M' or the number of gigabytes postfixed by 'G'. If |
| 393 |
* representing K, M or G, and the number is amount is less than 100, |
| 394 |
* it includes a '.' plus the remainer. The returned string will be 5 |
| 395 |
* characters wide (padding with whitespace at the front) unless |
| 396 |
* negative or representing more than 9999G bytes. |
| 397 |
*/ |
| 398 |
function bytes_to_string:string (bytes:long) |
| 399 |
{ |
| 400 |
if (bytes < 1024) |
| 401 |
return sprintf("%5d", bytes); |
| 402 |
|
| 403 |
remain = bytes % 1024; |
| 404 |
bytes = bytes / 1024; |
| 405 |
if (bytes < 1024) |
| 406 |
return _stp_number_to_string_postfix(bytes, remain, "K"); |
| 407 |
|
| 408 |
remain = bytes % 1024; |
| 409 |
bytes = bytes / 1024; |
| 410 |
if (bytes < 1024) |
| 411 |
return _stp_number_to_string_postfix(bytes, remain, "M"); |
| 412 |
|
| 413 |
remain = bytes % 1024; |
| 414 |
bytes = bytes / 1024; |
| 415 |
return _stp_number_to_string_postfix(bytes, remain, "G"); |
| 416 |
} |
| 417 |
|
| 418 |
/** |
| 419 |
* sfunction pages_to_string - Turns pages into a human readable string |
| 420 |
* |
| 421 |
* @pages: Number of pages to translate. |
| 422 |
* |
| 423 |
* Description: Multiplies pages by page_size() to get the number of |
| 424 |
* bytes and returns the result of bytes_to_string(). |
| 425 |
*/ |
| 426 |
function pages_to_string:string (pages:long) |
| 427 |
{ |
| 428 |
bytes = pages * mem_page_size(); |
| 429 |
return bytes_to_string (bytes); |
| 430 |
} |
| 431 |
|
| 432 |
/** |
| 433 |
* sfunction proc_mem_string - Human readable string of process memory usage |
| 434 |
* |
| 435 |
* Description: Returns a human readable string showing the size, rss, |
| 436 |
* shr, txt and data of the memory used by the current process. |
| 437 |
* For example "size: 301m, rss: 11m, shr: 8m, txt: 52k, data: 2248k". |
| 438 |
*/ |
| 439 |
function proc_mem_string:string () |
| 440 |
{ |
| 441 |
return sprintf ("size: %s, rss: %s, shr: %s, txt: %s, data: %s", |
| 442 |
pages_to_string(proc_mem_size()), |
| 443 |
pages_to_string(proc_mem_rss()), |
| 444 |
pages_to_string(proc_mem_shr()), |
| 445 |
pages_to_string(proc_mem_txt()), |
| 446 |
pages_to_string(proc_mem_data())); |
| 447 |
} |
| 448 |
|
| 449 |
/** |
| 450 |
* sfunction proc_mem_string - Human readable string of process memory usage |
| 451 |
* |
| 452 |
* @pid: The pid of process to examine |
| 453 |
* |
| 454 |
* Description: Returns a human readable string showing the size, rss, |
| 455 |
* shr, txt and data of the memory used by the given process. |
| 456 |
* For example "size: 301m, rss: 11m, shr: 8m, txt: 52k, data: 2248k". |
| 457 |
*/ |
| 458 |
function proc_mem_string:string (pid:long) |
| 459 |
{ |
| 460 |
return sprintf ("size: %s, rss: %s, shr: %s, txt: %s, data: %s", |
| 461 |
pages_to_string(proc_mem_size(pid)), |
| 462 |
pages_to_string(proc_mem_rss(pid)), |
| 463 |
pages_to_string(proc_mem_shr(pid)), |
| 464 |
pages_to_string(proc_mem_txt(pid)), |
| 465 |
pages_to_string(proc_mem_data(pid))); |
| 466 |
} |
| 467 |
function proc_mem_string_pid:string (pid:long) |
| 468 |
{ |
| 469 |
return proc_mem_string(pid) |
| 470 |
} |