Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / glue / vmsize-glue.c
blobc37a7699cffd7cc49032d4778f82db4e144bc674
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /*
4 * vmsize-glue.c
6 * Copyright (C) 2004 Novell, Inc.
8 */
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 * DEALINGS IN THE SOFTWARE.
30 #include <string.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <stdlib.h>
36 #include <stdio.h>
39 FIXME: It is not safe to call this function from multiple threads.
42 int
43 get_vmsize (void)
45 static char proc_filename[64] = {'\0'};
46 static char buffer [1024];
47 int fd;
48 int vmsize = -1;
50 if (proc_filename[0] == '\0')
51 snprintf (proc_filename, 64, "/proc/%d/status", getpid ());
53 fd = open (proc_filename, O_RDONLY);
54 if (fd >= 0) {
55 if (read (fd, buffer, sizeof (buffer)) > 0) {
56 char *pos = strstr (buffer, "VmSize:");
57 char *endpos = NULL;
58 if (pos != NULL && strlen (pos) > 7) {
59 pos += 7;
60 while (*pos && isspace (*pos))
61 ++pos;
62 if (*pos != '\0') {
63 vmsize = (int) strtol (pos, &endpos, 10);
64 if (pos == endpos || *endpos != ' ')
65 vmsize = -1;
69 close (fd);
72 return vmsize;
75 /* a stupid cut&paste */
76 int
77 get_vmrss (void)
79 static char proc_filename[64] = {'\0'};
80 static char buffer [1024];
81 int fd;
82 int vmsize = -1;
84 if (proc_filename[0] == '\0')
85 snprintf (proc_filename, 64, "/proc/%d/status", getpid ());
87 fd = open (proc_filename, O_RDONLY);
88 if (fd >= 0) {
89 if (read (fd, buffer, sizeof (buffer)) > 0) {
90 char *pos = strstr (buffer, "VmRSS:");
91 char *endpos = NULL;
92 if (pos != NULL && strlen (pos) > 7) {
93 pos += 7;
94 while (*pos && isspace (*pos))
95 ++pos;
96 if (*pos != '\0') {
97 vmsize = (int) strtol (pos, &endpos, 10);
98 if (pos == endpos || *endpos != ' ')
99 vmsize = -1;
103 close (fd);
106 return vmsize;