Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
load.h File Reference
#include <time.h>

Go to the source code of this file.

Macros

#define load_create()   load_create_real(1)
 
#define load_increase(load)   load_increase_with(load, 1)
 

Typedefs

typedef struct load Load
 

Functions

Loadload_create_real (int heuristic)
 
int load_add_interval (Load *load, int interval)
 
void load_increase_with (Load *load, unsigned long value)
 
void load_destroy (Load *load)
 
double load_get (Load *load, int pos)
 
int load_len (Load *load)
 

Macro Definition Documentation

#define load_create ( )    load_create_real(1)

Definition at line 78 of file load.h.

Referenced by init_bearerbox(), and smscconn_create().

#define load_increase (   load)    load_increase_with(load, 1)

Typedef Documentation

typedef struct load Load

load.h

Alexander Malysh <amalysh at kannel.org> 2008 for project Kannel Anonymos Load typedef.

Definition at line 71 of file load.h.

Function Documentation

int load_add_interval ( Load load,
int  interval 
)

Add load measure interval. - load object - measure interval in seconds

Returns
-1 if error occurs (e.g. interval already exists); 0 if all was fine

Definition at line 111 of file load.c.

References load_entry::curr, load_entry::dirty, load::entries, gw_rwlock_unlock(), gw_rwlock_wrlock(), load_entry::interval, interval, load_entry::last, load::len, load::lock, microtime(), and load_entry::prev.

Referenced by init_bearerbox(), smpp_create(), smsc_at2_create(), and smscconn_create().

112 {
113  int i;
114  struct load_entry *entry;
115 
116  if (load == NULL)
117  return -1;
118 
119  gw_rwlock_wrlock(load->lock);
120 
121  /* first look if we have equal interval added already */
122  for (i = 0; i < load->len; i++) {
123  if (load->entries[i]->interval == interval) {
124  gw_rwlock_unlock(load->lock);
125  return -1;
126  }
127  }
128  /* so no equal interval there, add new one */
129  entry = gw_malloc(sizeof(struct load_entry));
130  entry->prev = entry->curr = 0.0;
131  entry->interval = interval;
132  entry->dirty = 1;
133  microtime(&entry->last);
134 
135  load->entries = gw_realloc(load->entries, sizeof(struct load*) * (load->len + 1));
136  load->entries[load->len] = entry;
137  load->len++;
138 
139  gw_rwlock_unlock(load->lock);
140 
141  return 0;
142 }
float curr
Definition: load.c:69
float prev
Definition: load.c:68
int gw_rwlock_wrlock(RWLock *lock)
Definition: gw-rwlock.c:177
struct load_entry ** entries
Definition: load.c:77
RWLock * lock
Definition: load.c:80
int len
Definition: load.c:78
int interval
Definition: load.c:71
double last
Definition: load.c:70
int gw_rwlock_unlock(RWLock *lock)
Definition: gw-rwlock.c:155
double interval
Definition: fakewap.c:234
Definition: load.c:67
static double microtime(double *p)
Definition: load.c:83
int dirty
Definition: load.c:72
Load* load_create_real ( int  heuristic)

Create new Load object. - 0 disable heuristic (means get always current load); 1 enable

Definition at line 97 of file load.c.

References load::entries, gw_rwlock_create(), load::heuristic, load::len, and load::lock.

Referenced by smpp_create(), and smsc_at2_create().

98 {
99  struct load *load;
100 
101  load = gw_malloc(sizeof(*load));
102  load->len = 0;
103  load->entries = NULL;
104  load->heuristic = heuristic;
105  load->lock = gw_rwlock_create();
106 
107  return load;
108 }
RWLock * gw_rwlock_create(void)
Definition: gw-rwlock.c:77
struct load_entry ** entries
Definition: load.c:77
RWLock * lock
Definition: load.c:80
int len
Definition: load.c:78
Definition: load.c:76
int heuristic
Definition: load.c:79
void load_destroy ( Load load)

Destroy load object. - load object

Definition at line 145 of file load.c.

References load::entries, gw_rwlock_destroy(), load::len, and load::lock.

Referenced by at2_device_thread(), empty_msg_lists(), smpp_destroy(), and smscconn_destroy().

146 {
147  int i;
148 
149  if (load == NULL)
150  return;
151 
152  for (i = 0; i < load->len; i++) {
153  gw_free(load->entries[i]);
154  }
155  gw_free(load->entries);
156  gw_rwlock_destroy(load->lock);
157  gw_free(load);
158 }
void gw_rwlock_destroy(RWLock *lock)
Definition: gw-rwlock.c:112
struct load_entry ** entries
Definition: load.c:77
RWLock * lock
Definition: load.c:80
int len
Definition: load.c:78
double load_get ( Load load,
int  pos 
)

Get measured load value at position .

Definition at line 191 of file load.c.

References load_entry::curr, load_entry::dirty, load::entries, gw_rwlock_rdlock(), gw_rwlock_unlock(), load::heuristic, load_entry::last, load::len, load_increase_with(), load::lock, microtime(), and load_entry::prev.

Referenced by at2_send_messages(), bb_print_status(), send_messages(), and smsc2_status().

192 {
193  double ret;
194  double now;
195  struct load_entry *entry;
196 
197  if (load == NULL || pos >= load->len) {
198  return -1.0;
199  }
200 
201  /* first maybe rotate load */
202  load_increase_with(load, 0);
203 
204  microtime(&now);
205  gw_rwlock_rdlock(load->lock);
206  entry = load->entries[pos];
207  if (load->heuristic && !entry->dirty) {
208  ret = entry->prev;
209  } else {
210  double diff = (now - entry->last);
211  if (diff == 0) diff = 1;
212  ret = entry->curr/diff;
213  ret = (ret > entry->curr ? entry->curr : ret);
214  }
215  gw_rwlock_unlock(load->lock);
216 
217  return ret;
218 }
float curr
Definition: load.c:69
float prev
Definition: load.c:68
int gw_rwlock_rdlock(RWLock *lock)
Definition: gw-rwlock.c:134
struct load_entry ** entries
Definition: load.c:77
RWLock * lock
Definition: load.c:80
int len
Definition: load.c:78
double last
Definition: load.c:70
int gw_rwlock_unlock(RWLock *lock)
Definition: gw-rwlock.c:155
Definition: load.c:67
int heuristic
Definition: load.c:79
static double microtime(double *p)
Definition: load.c:83
void load_increase_with(Load *load, unsigned long value)
Definition: load.c:161
int dirty
Definition: load.c:72
void load_increase_with ( Load load,
unsigned long  value 
)

Increase load values with . - load object - how much to increase

Definition at line 161 of file load.c.

References load_entry::curr, load_entry::dirty, load::entries, gw_rwlock_unlock(), gw_rwlock_wrlock(), load_entry::interval, load_entry::last, load::len, load::lock, microtime(), and load_entry::prev.

Referenced by load_get().

162 {
163  double now;
164  int i;
165 
166  if (load == NULL)
167  return;
168 
169  gw_rwlock_wrlock(load->lock);
170  microtime(&now);
171  for (i = 0; i < load->len; i++) {
172  struct load_entry *entry = load->entries[i];
173  /* check for special case, load over whole live time */
174  if((entry->interval != -1 && now >= entry->last + entry->interval)) {
175  /* rotate */
176  entry->curr /= entry->interval;
177  if (entry->prev > 0)
178  entry->prev = (2*entry->curr + entry->prev)/3;
179  else
180  entry->prev = entry->curr;
181  entry->last = now;
182  entry->curr = 0.0;
183  entry->dirty = 0;
184  }
185  entry->curr += value;
186  }
187  gw_rwlock_unlock(load->lock);
188 }
float curr
Definition: load.c:69
float prev
Definition: load.c:68
int gw_rwlock_wrlock(RWLock *lock)
Definition: gw-rwlock.c:177
struct load_entry ** entries
Definition: load.c:77
RWLock * lock
Definition: load.c:80
int len
Definition: load.c:78
int interval
Definition: load.c:71
double last
Definition: load.c:70
int gw_rwlock_unlock(RWLock *lock)
Definition: gw-rwlock.c:155
Definition: load.c:67
static double microtime(double *p)
Definition: load.c:83
int dirty
Definition: load.c:72
int load_len ( Load load)

Get length of intervals.

Definition at line 221 of file load.c.

References gw_rwlock_rdlock(), gw_rwlock_unlock(), load::len, and load::lock.

222 {
223  int ret;
224  if (load == NULL)
225  return 0;
226  gw_rwlock_rdlock(load->lock);
227  ret = load->len;
228  gw_rwlock_unlock(load->lock);
229  return ret;
230 }
int gw_rwlock_rdlock(RWLock *lock)
Definition: gw-rwlock.c:134
RWLock * lock
Definition: load.c:80
int len
Definition: load.c:78
int gw_rwlock_unlock(RWLock *lock)
Definition: gw-rwlock.c:155
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.