summaryrefslogtreecommitdiffstats
path: root/tapset/proc_mem.stp
blob: 28bd13a752107d26a3e22b5bb76d5e0c29e140ca (plain)
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
// Process memory query and utility functions.
// Copyright (C) 2009, 2010 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.

// <tapsetdescription>
// Process memory query and utility functions provide information about
// the memory usage of the current application. These functions provide
// information about the full size, resident, shared, code and data used
// by the current process. And provide utility functions to query the
// page size of the current architecture and create human readable string
// representations of bytes and pages used.
// </tapsetdescription>

%{
/* PF_BORROWED_MM got renamed to PF_KTHREAD with same semantics somewhere. */
#ifdef PF_BORROWED_MM
#define _STP_PF_KTHREAD PF_BORROWED_MM
#else
#define _STP_PF_KTHREAD PF_KTHREAD
#endif

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,34)
#include <linux/mm_types.h>
#else
/* Define our own mm types */
enum {
	MM_FILEPAGES,
	MM_ANONPAGES
};
#endif
%}

/* Try to be slightly paranoid. Only returns 1 if the task isn't
   starting, exiting or (coopted by) a kernel thread. */
function _stp_valid_task:long(tsk:long)
%{
  struct task_struct *tsk = (struct task_struct *)(long)THIS->tsk;

  THIS->__retvalue = 0;
  if (tsk) {
    unsigned int flags = kread(&(tsk->flags));

    if (flags & ~(_STP_PF_KTHREAD | PF_EXITING | PF_STARTING))
      THIS->__retvalue = 1;
  }
  CATCH_DEREF_FAULT();
%}

function _MM_FILEPAGES:long()
%{ /* pure */ /* unprivileged */
  THIS->__retvalue = MM_FILEPAGES;
%}

function _MM_ANONPAGES:long()
%{ /* pure */ /* unprivileged */
  THIS->__retvalue = MM_ANONPAGES;
%}

function _stp_get_mm_counter:long(mm:long, member:long)
{
%(kernel_v >= "2.6.34" %?
%( CONFIG_NR_CPUS >= CONFIG_SPLIT_PTLOCK_CPUS %?
  val = atomic_long_read(&@cast(mm, "mm_struct", "kernel<linux/sched.h>")->rss_stat->count[member])
%:
  val = @cast(mm, "mm_struct", "kernel<linux/sched.h>")->rss_stat->count[member]
%)
  if (val < 0)
    return 0
%:	/* kernel < 2.6.34 */
  if (member == _MM_FILEPAGES()) {
%( CONFIG_NR_CPUS >= CONFIG_SPLIT_PTLOCK_CPUS %?
    val = atomic_long_read(&@cast(mm, "mm_struct", "kernel<linux/sched.h>")->_file_rss)
%:
    val = @cast(mm, "mm_struct", "kernel<linux/sched.h>")->_file_rss
%)
  }
  else if (member == _MM_ANONPAGES()) {
%( CONFIG_NR_CPUS >= CONFIG_SPLIT_PTLOCK_CPUS %?
    val = atomic_long_read(&@cast(mm, "mm_struct", "kernel<linux/sched.h>")->_anon_rss)
%:
    val = @cast(mm, "mm_struct", "kernel<linux/sched.h>")->_anon_rss
%)
  }
%)
  return val
}

/**
 * sfunction proc_mem_size - Total program virtual memory size in pages
 *
 * Description: Returns the total virtual memory size in pages of the
 * current process, or zero when there is no current process or the
 * number of pages couldn't be retrieved.
 */
function proc_mem_size:long ()
{
   task = task_current()
   if (_stp_valid_task(task)) {
     mm = @cast(task, "task_struct", "kernel<linux/sched.h>")->mm
     if (mm != 0)
       return @cast(mm, "mm_struct", "kernel<linux/sched.h>")->total_vm
   }
   return 0
}

/**
 * sfunction proc_mem_size_pid - Total program virtual memory size in pages
 *
 * @pid: The pid of process to examine
 *
 * Description: Returns the total virtual memory size in pages of the
 * given process, or zero when that process doesn't exist or the
 * number of pages couldn't be retrieved.
 */
function proc_mem_size_pid:long (pid:long)
{
  task = pid2task(pid)
  if (_stp_valid_task(task)) {
    mm = @cast(task, "task_struct", "kernel<linux/sched.h>")->mm
    if (mm != 0)
      return @cast(mm, "mm_struct", "kernel<linux/sched.h>")->total_vm
  }
  return 0
}

/**
 * sfunction proc_mem_rss - Program resident set size in pages
 *
 * Description: Returns the resident set size in pages of the current
 * process, or zero when there is no current process or the number of
 * pages couldn't be retrieved.
 */
function proc_mem_rss:long ()
{
  task = task_current()
  if (_stp_valid_task(task)) {
    mm = @cast(task, "task_struct", "kernel<linux/sched.h>")->mm
    if (mm != 0)
      return (_stp_get_mm_counter(mm, _MM_FILEPAGES())
	      + _stp_get_mm_counter(mm, _MM_ANONPAGES()))
  }
  return 0
}

/**
 * sfunction proc_mem_rss_pid - Program resident set size in pages
 *
 * @pid: The pid of process to examine
 *
 * Description: Returns the resident set size in pages of the given
 * process, or zero when the process doesn't exist or the number of
 * pages couldn't be retrieved.
 */
function proc_mem_rss_pid:long (pid:long)
{
  task = pid2task(pid)
  if (_stp_valid_task(task)) {
    mm = @cast(task, "task_struct", "kernel<linux/sched.h>")->mm
    if (mm != 0)
        return (_stp_get_mm_counter(mm, _MM_FILEPAGES())
                + _stp_get_mm_counter(mm, _MM_ANONPAGES()))
  }
  return 0
}

/**
 * sfunction proc_mem_shr - Program shared pages (from shared mappings)
 *
 * Description: Returns the shared pages (from shared mappings) of the
 * current process, or zero when there is no current process or the
 * number of pages couldn't be retrieved.
 */
function proc_mem_shr:long ()
{
  task = task_current()
  if (_stp_valid_task(task)) {
    mm = @cast(task, "task_struct", "kernel<linux/sched.h>")->mm
    if (mm != 0)
      return _stp_get_mm_counter(mm, _MM_FILEPAGES())
  }
  return 0
}

/**
 * sfunction proc_mem_shr_pid - Program shared pages (from shared mappings)
 *
 * @pid: The pid of process to examine
 *
 * Description: Returns the shared pages (from shared mappings) of the
 * given process, or zero when the process doesn't exist or the
 * number of pages couldn't be retrieved.
 */
function proc_mem_shr_pid:long (pid:long)
{
  task = pid2task(pid)
  if (_stp_valid_task(task)) {
    mm = @cast(task, "task_struct", "kernel<linux/sched.h>")->mm
    if (mm != 0)
      return _stp_get_mm_counter(mm, _MM_FILEPAGES())
  }
  return 0
}

function _stp_mem_txt_adjust:long (start_code:long, end_code:long)
%{ /* pure */
  unsigned long start_code = (unsigned long) THIS->start_code;
  unsigned long end_code = (unsigned long) THIS->end_code;
  THIS->__retvalue = (PAGE_ALIGN(end_code)
                      - (start_code & PAGE_MASK)) >> PAGE_SHIFT;
%}

/**
 * sfunction proc_mem_txt - Program text (code) size in pages
 *
 * Description: Returns the current process text (code) size in pages,
 * or zero when there is no current process or the number of pages
 * couldn't be retrieved.
 */
function proc_mem_txt:long ()
{
  task = task_current()
  if (_stp_valid_task(task)) {
    mm = @cast(task, "task_struct", "kernel<linux/sched.h>")->mm
    if (mm != 0) {
      s = @cast(mm, "mm_struct", "kernel<linux/sched.h>")->start_code
      e = @cast(mm, "mm_struct", "kernel<linux/sched.h>")->end_code
      return _stp_mem_txt_adjust(s, e)
    }
  }
  return 0
}

/**
 * sfunction proc_mem_txt_pid - Program text (code) size in pages
 *
 * @pid: The pid of process to examine
 *
 * Description: Returns the given process text (code) size in pages,
 * or zero when the process doesn't exist or the number of pages
 * couldn't be retrieved.
 */
function proc_mem_txt_pid:long (pid:long)
{
  task = pid2task(pid)
  if (_stp_valid_task(task)) {
    mm = @cast(task, "task_struct", "kernel<linux/sched.h>")->mm
    if (mm != 0) {
      s = @cast(mm, "mm_struct", "kernel<linux/sched.h>")->start_code
      e = @cast(mm, "mm_struct", "kernel<linux/sched.h>")->end_code
      return _stp_mem_txt_adjust (s, e)
    }
  }
  return 0
}

/**
 * sfunction proc_mem_data - Program data size (data + stack) in pages
 *
 * Description: Returns the current process data size (data + stack)
 * in pages, or zero when there is no current process or the number of
 * pages couldn't be retrieved.
 */
function proc_mem_data:long ()
{
  task = task_current()
  if (_stp_valid_task(task)) {
    mm = @cast(task, "task_struct", "kernel<linux/sched.h>")->mm
    if (mm != 0) {
      t = @cast(mm, "mm_struct", "kernel<linux/sched.h>")->total_vm
      s = @cast(mm, "mm_struct", "kernel<linux/sched.h>")->shared_vm
      return (t - s)
    }
  }
  return 0
}

/**
 * sfunction proc_mem_data_pid - Program data size (data + stack) in pages
 *
 * @pid: The pid of process to examine
 *
 * Description: Returns the given process data size (data + stack)
 * in pages, or zero when the process doesn't exist or the number of
 * pages couldn't be retrieved.
 */
function proc_mem_data_pid:long (pid:long)
{
  task = pid2task(pid)
  if (_stp_valid_task(task)) {
    mm = @cast(task, "task_struct", "kernel<linux/sched.h>")->mm
    if (mm != 0) {
      t = @cast(mm, "mm_struct", "kernel<linux/sched.h>")->total_vm
      s = @cast(mm, "mm_struct", "kernel<linux/sched.h>")->shared_vm
      return t - s
    }
  }
  return 0
}

/**
 * sfunction mem_page_size - Number of bytes in a page for this architecture
 */
function mem_page_size:long ()
%{ /* pure */ /* unprivileged */
   THIS->__retvalue = PAGE_SIZE;
%}

// Return a 5 character wide string " x.yyp", " xx.yp", " xxxp", "xxxxp".
function _stp_number_to_string_postfix:string (x:long, y:long, p:string)
{
  if (x < 10)
    return sprintf("%d.%.2d%s", x, y * 100 / 1024, p);
  if (x < 100)
    return sprintf("%2d.%d%s", x, y * 10 / 1024, p);
  return sprintf("%4d%s", x, p);
}

/**
 * sfunction bytes_to_string - Human readable string for given bytes
 *
 * @bytes: Number of bytes to translate.
 *
 * Description: Returns a string representing the number of bytes (up
 * to 1024 bytes), the number of kilobytes (when less than 1024K)
 * postfixed by 'K', the number of megabytes (when less than 1024M)
 * postfixed by 'M' or the number of gigabytes postfixed by 'G'. If
 * representing K, M or G, and the number is amount is less than 100,
 * it includes a '.' plus the remainer. The returned string will be 5
 * characters wide (padding with whitespace at the front) unless
 * negative or representing more than 9999G bytes.
 */
function bytes_to_string:string (bytes:long)
{
  if (bytes < 1024)
    return sprintf("%5d", bytes);

  remain = bytes % 1024;
  bytes = bytes / 1024;
  if (bytes < 1024)
    return _stp_number_to_string_postfix(bytes, remain, "K");

  remain = bytes % 1024;
  bytes = bytes / 1024;
  if (bytes < 1024)
    return _stp_number_to_string_postfix(bytes, remain, "M");

  remain = bytes % 1024;
  bytes = bytes / 1024;
    return _stp_number_to_string_postfix(bytes, remain, "G");
}

/**
 * sfunction pages_to_string - Turns pages into a human readable string
 *
 * @pages: Number of pages to translate.
 *
 * Description: Multiplies pages by page_size() to get the number of
 * bytes and returns the result of bytes_to_string().
 */
function pages_to_string:string (pages:long)
{
  bytes = pages * mem_page_size();
  return bytes_to_string (bytes);
}

/**
 * sfunction proc_mem_string - Human readable string of current proc memory usage
 *
 * Description: Returns a human readable string showing the size, rss,
 * shr, txt and data of the memory used by the current process.
 * For example "size: 301m, rss: 11m, shr: 8m, txt: 52k, data: 2248k".
 */
function proc_mem_string:string ()
{
  return sprintf ("size: %s, rss: %s, shr: %s, txt: %s, data: %s",
                  pages_to_string(proc_mem_size()),
                  pages_to_string(proc_mem_rss()),
                  pages_to_string(proc_mem_shr()),
                  pages_to_string(proc_mem_txt()),
                  pages_to_string(proc_mem_data()));
}

/**
 * sfunction proc_mem_string_pid - Human readable string of process memory usage
 *
 * @pid: The pid of process to examine
 *
 * Description: Returns a human readable string showing the size, rss,
 * shr, txt and data of the memory used by the given process.
 * For example "size: 301m, rss: 11m, shr: 8m, txt: 52k, data: 2248k".
 */
function proc_mem_string_pid:string (pid:long)
{
  return sprintf ("size: %s, rss: %s, shr: %s, txt: %s, data: %s",
                  pages_to_string(proc_mem_size_pid(pid)),
                  pages_to_string(proc_mem_rss_pid(pid)),
                  pages_to_string(proc_mem_shr_pid(pid)),
                  pages_to_string(proc_mem_txt_pid(pid)),
                  pages_to_string(proc_mem_data_pid(pid)));
}