File: hash.c

package info (click to toggle)
gmemusage 0.2-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 260 kB
  • sloc: ansic: 3,227; perl: 67; makefile: 59; sh: 22
file content (152 lines) | stat: -rw-r--r-- 3,137 bytes parent folder | download
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
/*
 * hash.c
 * Process hashing functions for gmemusage.
 *
 * Copyright (C) 1997, 1998 by Raju Mathur (raju@sgi.com)
 *
 * See file COPYING (included in this distribution) for copyright information.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include "common.h"

/*
{
*/
   const int
      minProcs = 16 ;
   static struct ProcInfo
      *procs = NULL ;
   static struct ProcInfo
      *nextproc = NULL ;
   static int
      nProcs = 0 ;
   static int
      lastallocated ;
/*
}
*/
/*
 * Find a process with matching name in the process array.
 * Currently using linear search, enhance to any method you
 * choose. Hash may not be a bad idea.
 */
struct ProcInfo
*LookupProc ( char *name )
{
   register struct ProcInfo
      *pi ;

   for ( pi = procs ; pi - procs < nProcs ; pi++ )
   {
      if ( !strcmp ( pi -> procname , name ) )
      {
	 return ( pi ) ;
      }
   }
   return NULL ;
}
/*
 * Update existing process entry, or add a process to the
 * process array.
 */
void
addProc ( char *procname , int mem , int rss )
{
   register struct ProcInfo
      *thisproc ;
/*
 * is the array empty?
 */
   if ( !procs )
   {
      lastallocated = minProcs ;
      if ( ( procs = calloc ( minProcs , sizeof ( struct ProcInfo ) ) )
	   == NULL )
      {
	 fprintf ( stderr , "%s: cannot alloc %d processes" , progname ,
		   minProcs ) ;
	 perror ( "" ) ;
	 exit ( 1 ) ;
      }
/* printf("allocated %d procs\n",lastallocated); */
      thisproc = nextproc = procs ;
      strcpy ( thisproc -> procname , procname ) ;
      thisproc -> totMem = mem ;
      thisproc -> totRSS = rss ;
      thisproc -> nProcs = 1 ;
      nProcs = 1 ;
   }
/*
 * if a process with that name doesn't already exist in the
 * array, make a new entry. Allocate more space if necessary.
 */
   else if ( !( thisproc = LookupProc ( procname ) ) )
   {
      if ( nProcs == lastallocated )	/* no more space */
      {
	 lastallocated *= 2 ;
	 if ( ( procs = realloc
		( procs , lastallocated * sizeof ( struct ProcInfo) ) )
	      == NULL )
	 {
	    fprintf ( stderr , "%s: cannot alloc %d processes" ,
		      progname , lastallocated ) ;
	    perror ( "" ) ;
	    exit ( 1 ) ;
	 }
/* printf("allocated %d procs\n",lastallocated); */
      }
      thisproc = procs + nProcs ;
      strcpy ( thisproc -> procname , procname ) ;
      thisproc -> totMem = mem ;
      thisproc -> totRSS = rss ;
      thisproc -> nProcs = 1 ;
      nProcs++ ;
   }
   else
   {
      thisproc -> totMem += mem ;
      thisproc -> totRSS += rss ;
      thisproc -> nProcs++ ;
   }
}
/*
 * Return the next process linearly from the process
 * array, or NULL if no more.
 */
struct ProcInfo
*NextProc ( void )
{
   if ( nextproc - procs == nProcs )
   {
      return NULL ;
   }
   else
   {
      return nextproc++ ;
   }
}
struct ProcInfo
*AllProcs ( int *n )
{
   *n = nProcs ;
   return procs ;
}
/*
 * Clear out and deallocate the process list and
 * associated variables.
 */
void
ClearProcs ( void )
{
   if ( procs )
   {
      free ( procs ) ;
      procs = NULL ;
      lastallocated = nProcs = 0 ;
      nextproc = NULL ;
   }
}