~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/libs/sgeobj/sge_usage.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*___INFO__MARK_BEGIN__*/
 
2
/*************************************************************************
 
3
 * 
 
4
 *  The Contents of this file are made available subject to the terms of
 
5
 *  the Sun Industry Standards Source License Version 1.2
 
6
 * 
 
7
 *  Sun Microsystems Inc., March, 2001
 
8
 * 
 
9
 * 
 
10
 *  Sun Industry Standards Source License Version 1.2
 
11
 *  =================================================
 
12
 *  The contents of this file are subject to the Sun Industry Standards
 
13
 *  Source License Version 1.2 (the "License"); You may not use this file
 
14
 *  except in compliance with the License. You may obtain a copy of the
 
15
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 
16
 * 
 
17
 *  Software provided under this License is provided on an "AS IS" basis,
 
18
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 
19
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 
20
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 
21
 *  See the License for the specific provisions governing your rights and
 
22
 *  obligations concerning the Software.
 
23
 * 
 
24
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 
25
 * 
 
26
 *   Copyright: 2001 by Sun Microsystems, Inc.
 
27
 * 
 
28
 *   All Rights Reserved.
 
29
 * 
 
30
 ************************************************************************/
 
31
/*___INFO__MARK_END__*/
 
32
 
 
33
#include <string.h>
 
34
 
 
35
#include "cull.h"
 
36
#include "sge_usage.h"
 
37
 
 
38
/****** sgeobj/usage/usage_list_get_ulong_usage() *****************************
 
39
*  NAME
 
40
*     usage_list_get_ulong_usage() -- return ulong usage value
 
41
*
 
42
*  SYNOPSIS
 
43
*     u_long32 
 
44
*     usage_list_get_ulong_usage(const lList *usage_list, const char *name, 
 
45
*                                u_long32 def) 
 
46
*
 
47
*  FUNCTION
 
48
*     Searches a usage object with the given name in the given usage 
 
49
*     list. If such an element is found, returns the value of the 
 
50
*     usage object as u_long32 value.
 
51
*     If no such element is found, return the given default value.
 
52
*
 
53
*  INPUTS
 
54
*     const lList *usage_list - the usage list
 
55
*     const char *name        - name of the element to search
 
56
*     u_long32 def            - default value
 
57
*
 
58
*  RESULT
 
59
*     u_long32 - value of found object or default
 
60
*
 
61
*  SEE ALSO
 
62
*     gdi/usage/usage_list_get_double_usage()
 
63
*******************************************************************************/
 
64
u_long32
 
65
usage_list_get_ulong_usage(const lList *usage_list, const char *name,
 
66
                           u_long32 def)
 
67
{
 
68
   lListElem *ep = lGetElemStr(usage_list, UA_name, name);
 
69
   if (ep != NULL) {
 
70
      return (u_long32)lGetDouble(ep, UA_value);
 
71
   } else {
 
72
      return def;
 
73
   }
 
74
}
 
75
 
 
76
/****** sgeobj/usage/usage_list_get_double_usage() ****************************
 
77
*  NAME
 
78
*     usage_list_get_double_usage() -- return double usage value
 
79
*
 
80
*  SYNOPSIS
 
81
*     double
 
82
*     usage_list_get_double_usage(const lList *usage_list, const char *name, 
 
83
*                                 double def) 
 
84
*
 
85
*  FUNCTION
 
86
*     Searches a usage object with the given name in the given usage 
 
87
*     list. If such an element is found, returns the value of the 
 
88
*     usage object as double value.
 
89
*     If no such element is found, return the given default value.
 
90
*
 
91
*  INPUTS
 
92
*     const lList *usage_list - the usage list
 
93
*     const char *name        - name of the element to search
 
94
*     double def              - default value
 
95
*
 
96
*  RESULT
 
97
*     double - value of found object or default
 
98
*
 
99
*  SEE ALSO
 
100
*     gdi/usage/usage_list_get_ulong_usage()
 
101
*******************************************************************************/
 
102
double
 
103
usage_list_get_double_usage(const lList *usage_list, const char *name,
 
104
                            double def)
 
105
{
 
106
   lListElem *ep = lGetElemStr(usage_list, UA_name, name);
 
107
   if(ep != NULL) {
 
108
      return lGetDouble(ep, UA_value);
 
109
   } else {
 
110
      return def;
 
111
   }
 
112
}
 
113
 
 
114
/****** sgeobj/usage/usage_list_set_ulong_usage() ******************************
 
115
*  NAME
 
116
*     usage_list_set_ulong_usage() -- create/update a usage record
 
117
*
 
118
*  SYNOPSIS
 
119
*     void
 
120
*     usage_list_set_ulong_usage(lList *usage_list, const char *name, 
 
121
*                                u_long32 value) 
 
122
*
 
123
*  FUNCTION
 
124
*     Updates the value of a usage record. If no usage record exists with the
 
125
*     given name in usage_list, a new record is created.
 
126
*
 
127
*  INPUTS
 
128
*     lList *usage_list - list containing the usage record to update
 
129
*     const char *name  - name of the usage record to update
 
130
*     u_long32 value    - the new value
 
131
*
 
132
*  NOTES
 
133
*     MT-NOTE: usage_list_set_ulong_usage() is MT safe 
 
134
*
 
135
*  SEE ALSO
 
136
*     sgeobj/usage/usage_list_set_double_usage()
 
137
*     sgeobj/usage/usage_list_get_ulong_usage()
 
138
*     sgeobj/usage/usage_list_get_double_usage()
 
139
*******************************************************************************/
 
140
void
 
141
usage_list_set_ulong_usage(lList *usage_list, const char *name, u_long32 value)
 
142
{
 
143
   usage_list_set_double_usage(usage_list, name, value);
 
144
}
 
145
 
 
146
/****** sgeobj/usage/usage_list_set_double_usage() ******************************
 
147
*  NAME
 
148
*     usage_list_set_double_usage() -- create/update a usage record
 
149
*
 
150
*  SYNOPSIS
 
151
*     void
 
152
*     usage_list_set_double_usage(lList *usage_list, const char *name, 
 
153
*                                 double value) 
 
154
*
 
155
*  FUNCTION
 
156
*     Updates the value of a usage record. If no usage record exists with the
 
157
*     given name in usage_list, a new record is created.
 
158
*
 
159
*  INPUTS
 
160
*     lList *usage_list - list containing the usage record to update
 
161
*     const char *name  - name of the usage record to update
 
162
*     double value      - the new value
 
163
*
 
164
*  NOTES
 
165
*     MT-NOTE: usage_list_set_double_usage() is MT safe 
 
166
*
 
167
*  SEE ALSO
 
168
*     sgeobj/usage/usage_list_set_ulong_usage()
 
169
*     sgeobj/usage/usage_list_get_ulong_usage()
 
170
*     sgeobj/usage/usage_list_get_double_usage()
 
171
*******************************************************************************/
 
172
void
 
173
usage_list_set_double_usage(lList *usage_list, const char *name, double value)
 
174
{
 
175
   lListElem *ep = lGetElemStr(usage_list, UA_name, name);
 
176
   if (ep == NULL) {
 
177
      ep = lAddElemStr(&usage_list, UA_name, name, UA_Type);
 
178
   }
 
179
 
 
180
   lSetDouble(ep, UA_value, value);
 
181
}
 
182
 
 
183
/****** sge_usage/usage_list_sum() *********************************************
 
184
*  NAME
 
185
*     usage_list_sum() -- sum up usage of two lists
 
186
*
 
187
*  SYNOPSIS
 
188
*     void 
 
189
*     usage_list_sum(lList *usage_list, const lList *add_usage_list) 
 
190
*
 
191
*  FUNCTION
 
192
*     Add the usage reported in add_usage_list to usage_list.
 
193
*     Summing up of usage will only be done for certain attributes:
 
194
*        - cpu
 
195
*        - io
 
196
*        - iow
 
197
*        - mem
 
198
*        - vmem
 
199
*        - maxvmem
 
200
*        - all ru_* attributes (see man getrusage.2)
 
201
*
 
202
*  INPUTS
 
203
*     lList *usage_list           - the usage list to contain all usage
 
204
*     const lList *add_usage_list - usage to add to usage_list
 
205
*
 
206
*  NOTES
 
207
*     MT-NOTE: usage_list_sum() is MT safe 
 
208
*******************************************************************************/
 
209
void
 
210
usage_list_sum(lList *usage_list, const lList *add_usage_list)
 
211
{
 
212
   const lListElem *usage;
 
213
 
 
214
   for_each(usage, add_usage_list) {
 
215
      const char *name = lGetString(usage, UA_name);
 
216
      /* Sum up all usage attributes. */
 
217
      if (strcmp(name, USAGE_ATTR_CPU) == 0 ||
 
218
          strcmp(name, USAGE_ATTR_IO) == 0 ||
 
219
          strcmp(name, USAGE_ATTR_IOW) == 0 ||
 
220
          strcmp(name, USAGE_ATTR_VMEM) == 0 ||
 
221
          strcmp(name, USAGE_ATTR_MAXVMEM) == 0 ||
 
222
          strcmp(name, USAGE_ATTR_MEM) == 0 || 
 
223
          strncmp(name, "ru_", 3) == 0) {
 
224
         lListElem *sum = lGetElemStr(usage_list, UA_name, name);
 
225
         if (sum == NULL) {
 
226
            lAppendElem(usage_list, lCopyElem(usage));
 
227
         } else {
 
228
            lAddDouble(sum, UA_value, lGetDouble(usage, UA_value));
 
229
         }
 
230
      }   
 
231
   }
 
232
}
 
233