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 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
|
/* $Id: periodic.c,v 1.64 2004/11/13 08:53:31 graziano Exp $ */
#include "config_nws.h"
#include <unistd.h> /* getppid() */
#include <signal.h> /* kill() */
#include <stdio.h> /* sprintf() */
#include <stdlib.h>
#include <string.h> /* strcmp() strlen() */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "experiments.h" /* experiment storage and registration */
#include "host_protocol.h" /* MakeHostCookie() RegisterObject() */
#include "diagnostic.h" /* ABORT() FAIL() WARN() */
#include "messages.h" /* message facilities */
#include "osutil.h" /* CurrentTime() */
#include "skills.h" /* skill invocation */
#include "strutil.h" /* GETTOK() SAFESTRCPY() */
#include "periodic.h"
#include "nws_sensor.h"
#define DEFAULT_PERIOD "10"
typedef struct {
char name[63 + 1]; /* registration name */
int period; /* period of the activity */
pid_t pid; /* pid of the child */
KnownSkills skill; /* exercised skill */
char skillOptions[EXP_LIST_SIZE]; /* options for this instance */
unsigned long nextRun; /* next time we need to run */
} PeriodicActivity;
static void *lock = NULL; /* local lock */
/* Module globals. */
static PeriodicActivity *activities = NULL;
static int activityCount = 0;
static int doFork = 1; /* fork by deafult */
/*
* Handles a #header#.message periodic control message arrived on #sd#
* accompanied by #header#.
*/
static void
ProcessRequest(Socket *sd,
MessageHeader header) {
char *data;
DataDescriptor descriptor = SIMPLE_DATA(CHAR_TYPE, 0);
DataDescriptor measDescriptor = SIMPLE_DATA(DOUBLE_TYPE, 3);
double d[3];
NWSAPI_Measurement toStore;
switch(header.message) {
case PERIODIC_EXPERIMENT:
/* the first data are the measurement/value pair and the
* lenght of the series registration */
if(!RecvData(*sd, d, &measDescriptor, 1, -1)) {
ERROR("ProcessRequest: data receive failed\n");
break;
} else {
/* series registration will be here */
data = (char *)MALLOC((int)d[2] + 1);
if(data == NULL) {
ERROR("ProcessRequest: out of memory\n");
break;
}
descriptor.repetitions = (int)d[2];
if(!RecvData(*sd, data, &descriptor, 1, -1)) {
ERROR("ProcessRequest: data receive failed\n");
} else {
/* terminate the string */
data[(int)d[2]] = '\0';
/* got them: set the Experiment */
toStore.timeStamp = d[0];
toStore.measurement = d[1];
/* register the experiment */
RegisterExperiment(data, &toStore);
}
FREE(data);
}
break;
default:
DROP_SOCKET(sd);
ERROR1("ProcessRequest: unknown message %d\n", header.message);
break;
}
/* done with the socket */
if (*sd != NO_SOCKET) {
SocketIsAvailable(*sd);
}
}
/*
** Registers #periodic# with the name server.
*/
static void
RegisterActivity(const PeriodicActivity *periodic) {
int i, length;
char option[EXP_LIST_SIZE], res[EXP_LIST_SIZE], skill[EXP_NAME_SIZE];
const MeasuredResources *resources;
Object toRegister;
/* sanity check */
if (periodic == NULL) {
ERROR("RegisterActivity: NULL parameter\n");
return;
}
/* let's fill in the activity */
res[0] = '\0';
if (!SkillResources(periodic->skill, &resources, &length)) {
ERROR("RegisterActivity: failed to get info on skill\n");
return;
}
for(i = 0; i < length; i++) {
if(i > 0) {
strcat(res, "\t");
}
strcat(res, ResourceName(resources[i]));
}
SAFESTRCPY(skill, SkillName(periodic->skill));
sprintf(option, "period:%d", periodic->period);
if(periodic->skillOptions[0] != '\0') {
strcat(option, "\t");
strcat(option, periodic->skillOptions);
}
toRegister = CreateActivityObject(periodic->name,
PERIODIC_CONTROL_NAME,
EstablishedRegistration(),
option,
res,
skill);
RegisterObject(toRegister);
FreeObject(&toRegister);
}
void
PeriodicChildDeath(int pid) {
int i;
char *name, *opts;
KnownSkills skill;
/* something funk cold have happened, like the child unexpectedly
* died: we try to restart it here in that case. We are working
* on global variables so we need to lock everything up */
/* find the record for this child */
opts = name = NULL;
skill = 0;
GetNWSLock(&lock);
for(i = 0; i < activityCount; i++) {
if(activities[i].pid != pid) {
continue;
}
/* let's check we have a name */
if (activities[i].name == NULL) {
ERROR("PeriodicChildDeath: activity with no name\n");
} else {
LOG1("PeriodicChildDeath: %s was dead: restarting\n", activities[i].name);
/* save old value: we'll stop and restart the
* activity */
name = strdup(activities[i].name);
skill = activities[i].skill;
opts = strdup(activities[i].skillOptions);
if (name == NULL || opts == NULL) {
ERROR("PeriodicChildDeath: out of memory\n");
break;
}
}
}
ReleaseNWSLock(&lock);
/* let restart the activity if we need to */
if (name != NULL) {
StopPeriodicActivity(name);
StartPeriodicActivity(name, SkillName(skill), opts);
FREE(name);
FREE(opts);
}
}
int
PeriodicInit(const char *options) {
static int initialized = 0;
int i;
KnownSkills skill;
Object toRegister;
char *tmp, skills[EXP_LIST_SIZE], opts[EXP_LIST_SIZE];
/* initialize only once */
GetNWSLock(&lock);
if (initialized) {
ReleaseNWSLock(&lock);
ERROR("PeriodicInit: you can initialize only once!\n");
return 0;
}
initialized = 1;
ReleaseNWSLock(&lock);
/* we need to remember if we are forking */
tmp = GetOptionValue(options, "fork", NULL);
if (tmp == NULL || strncmp(tmp, "yes", (strlen(tmp) > 3 ? 3 : strlen(tmp))) == 0) {
doFork = 1;
} else {
doFork = 0;
}
FREE(tmp);
/* Register the periodic control. */
skills[0] = '\0';
for(i = 0; i < SKILL_COUNT; i++) {
skill = (KnownSkills) i;
if(!SkillAvailableForControl(skill, options, PERIODIC_CONTROL))
continue;
if (skills[0] != '\0')
strcat(skills, "\t");
strcat(skills, SkillName(skill));
}
SAFESTRCPY(opts, "period:0_to_1_int");
toRegister = CreateControlObject(NULL,
PERIODIC_CONTROL_NAME,
EstablishedRegistration(),
opts,
skills);
RegisterObject(toRegister);
FreeObject(&toRegister);
RegisterListener(PERIODIC_EXPERIMENT, "PERIODIC_EXPERIMENT", &ProcessRequest);
return 1;
}
double
NextPeriodicWork() {
int i;
double next;
next = 0.0;
GetNWSLock(&lock);
for (i = 0; i < activityCount; i++) {
/* if there is a child working on it, skip it */
if (activities[i].pid != 0) {
continue;
}
if (next == 0.0 || next >= activities[i].nextRun) {
next = activities[i].nextRun;
}
}
ReleaseNWSLock(&lock);
return next;
}
/* local function wrapper to UseSkill */
static int
DoWork( const PeriodicActivity *activity,
Socket childToParent) {
char *opts, *tmp;
int length, i, ret;
SkillResult *results;
double d[3];
NWSAPI_Measurement toStore;
Object toRegister;
DataDescriptor nameDescriptor = SIMPLE_DATA(CHAR_TYPE, 0);
DataDescriptor measDescriptor = SIMPLE_DATA(DOUBLE_TYPE, 3);
if (activity->skillOptions != NULL) {
opts = strdup(activity->skillOptions);
} else {
opts = strdup("");
}
if (opts == NULL) {
ERROR("DoWork: out of memory\n");
return 0;
}
/* add fork option if not already present */
tmp = GetOptionValue(activity->skillOptions, "fork", NULL);
if (tmp == NULL) {
opts = (char *)REALLOC(opts, strlen(opts) + 10);
if (opts == NULL) {
ERROR("DoWork: out of memory\n");
return 0;
}
if (opts[0] != '\0') {
strcat(opts, "\t");
}
sprintf(opts + strlen(opts), "fork:%s", (doFork) ? "yes" : "no");
}
FREE(tmp);
UseSkill(activity->skill,
opts,
activity->period,
&results,
&length);
FREE(opts);
/* let's fill in the fixed fields */
toStore.timeStamp = d[0] = CurrentTime();
/* pass the data to dad */
ret = 1;
for(i = 0; i < length && ret; i++) {
if(!results[i].succeeded) {
ret = 0;
WARN1("DoWork: %s failed\n", activity->name);
continue;
}
/* we need the generate the series registration */
toRegister = CreateSeriesObject(opts,
EstablishedRegistration(),
ResourceLabel(results[i].resource),
"noname",
results[i].options,
ResourceName(results[i].resource),
activity->name);
/* if we forked, we need to send a message to the parent ... */
if (doFork) {
/* let's tell the parent to store the
* experiment: the parent knows which
* memory is active right now, so it will
* do the right thing */
d[1] = results[i].measurement;
/* this is a trick: to keep the code
* simple we put the lenght of the
* series registration as 3 values here */
d[2] = strlen(toRegister);
nameDescriptor.repetitions = (int)d[2];
if (!SendMessageAndDatas(childToParent,
PERIODIC_EXPERIMENT,
d,
&measDescriptor,
1,
toRegister,
&nameDescriptor,
1,
0)) {
/* if we cannot talk to our
* parent there is no point on
* going ahead (we don't save the
* results directly) */
ret = 0;
ERROR("DoWork: failed to talk to parent\n");
}
} else {
/* ... otherwise we register ourself */
toStore.measurement = results[i].measurement;
RegisterExperiment(toRegister, &toStore);
}
/* free the memory */
FreeObject(&toRegister);
}
FreeSkillResults(length, &results);
return ret;
}
void
PeriodicWork(void) {
int i;
double now;
PeriodicActivity tmp;
now = CurrentTime();
GetNWSLock(&lock);
for (i = 0; i < activityCount; i++) {
/* if there is a child working on it, skip it */
if (activities[i].pid != 0) {
continue;
}
/* if it is no time yet, skip it */
if (activities[i].nextRun > now) {
continue;
}
/* given that activities is a global array, we need to
* copy into a local temp */
tmp = activities[i];
ReleaseNWSLock(&lock);
tmp.nextRun = now + tmp.period;
/* let's try to do the work */
DoWork(&tmp, NO_SOCKET);
/* get the lock and keep it around the for to protect
* access to activityCount */
GetNWSLock(&lock);
activities[i].nextRun = now + activities[i].period;
}
ReleaseNWSLock(&lock);
}
int
RecognizedPeriodicActivity(const char *name) {
int i, ret;
/* sanity check */
if (name == NULL) {
ERROR("RecognizedPeriodicActivity: NULL parameter\n");
return 0;
}
ret = 0;
GetNWSLock(&lock);
for(i = 0; i < activityCount && !ret; i++) {
ret = strcmp(activities[i].name, name) == 0;
}
ReleaseNWSLock(&lock);
return ret;
}
int
StartPeriodicActivity( const char *registration,
const char *skill,
const char *options) {
Socket childToParent;
int i, ret;
PeriodicActivity newActivity;
double nextRunTime;
struct timeval tv;
char *tmp;
/* sanity check */
if (skill == NULL || options == NULL) {
ERROR("StartPeriodicActivity: NULL parameters\n");
return 0;
}
/* let's see the name of this activity */
if (registration != NULL) {
SAFESTRCPY(newActivity.name, registration);
} else {
tmp = NameOfActivity_r(EstablishedRegistration(),
skill,
options);
SAFESTRCPY(newActivity.name, tmp);
free(tmp);
}
for(i = 0; i < SKILL_COUNT; i++) {
KnownSkills s = (KnownSkills) i;
if (!SkillAvailableForControl(s, options, PERIODIC_CONTROL)) {
continue;
}
if(strcmp(skill, SkillName(s)) == 0) {
newActivity.skill = s;
break;
}
}
/* let's check if we have the skill ability */
if(i == SKILL_COUNT) {
ERROR1("StartPeriodicActivity: Unsupported skill %s requested\n", skill);
return 0;
}
tmp = GetOptionValue(options, "period", DEFAULT_PERIOD);
newActivity.period = strtod(tmp, NULL);
FREE(tmp);
SkillOptions(newActivity.skill, options, newActivity.skillOptions, sizeof(newActivity.skillOptions));
/* next service is now */
newActivity.nextRun = 1;
/* let's see if we need to fork or not */
if (doFork) {
if(!CreateLocalChild(&newActivity.pid, NULL, &childToParent)) {
ERROR("StartPeriodicActivity: fork failed\n");
return 0;
}
} else {
/* non-forking: nothing to do here */
newActivity.pid = 0;
}
if((doFork && newActivity.pid != 0) || !doFork) {
/* Parent process. */
/* kill all childs already doing this activity */
if (StopPeriodicActivity(newActivity.name)) {
LOG("StartPeriodicActivity: stopped previous activity.\n");
}
GetNWSLock(&lock);
activityCount++;
activities = (PeriodicActivity *) REALLOC(activities, activityCount * sizeof(PeriodicActivity));
if (activities == NULL) {
ABORT("StartPeriodicActivity: out of memory\n");
}
activities[activityCount - 1] = newActivity;
ReleaseNWSLock(&lock);
RegisterActivity(&newActivity);
return 1;
}
/* sanity check: we shouldn't get here if we don't want to fork */
if (!doFork) {
ERROR("StartPeriodicActivity: forking when asked not to!\n");
return 0;
} else {
/* let's give some time for the parent to get ready to
* listen to us: got problems at least on Solaris 5.8 */
nextRunTime = CurrentTime() + 1;
}
/* Run as long as the parent process runs. */
for (ret = 1; ret ; ) {
/* let's go till our parent is available */
if (getppid() == 1) {
/* init is our parent: we are done here */
ret = 0;
continue;
}
/* let's sleep till the next exepriment. */
tv.tv_sec = nextRunTime - CurrentTime();
if(tv.tv_sec > 0) {
/* we use select instead of sleep because some
* system (ie condor) doesn't like us to use it */
tv.tv_usec = 0;
PortabilitySelect(0, NULL, NULL, NULL, &tv, NULL);
}
nextRunTime = CurrentTime() + newActivity.period;
/* let's try to do the work */
ret = DoWork(&newActivity, childToParent);
}
exit(0);
return 0;
}
int
StopPeriodicActivity(const char *registration) {
int i, j, ret;
/* sanity check */
if (registration == NULL) {
ERROR("StopPeriodicActivity: NULL parameter\n");
return 0;
}
ret = 0;
/* this is quite brutal: we lock the whole loop since we play
* with activities all along. Moreover if we are terminating and
* we are asked to stop we need to wait the lock! Let's by pass
* it when we are going to terminate */
if (registration[0] != '\0') {
GetNWSLock(&lock);
}
for(i = 0; i < activityCount; i++) {
/* if the registration is empty stop everything,
* otherwise look for the right activity */
if(registration[0] == '\0' || strcmp(activities[i].name, registration) == 0) {
if (activities[i].pid) {
/* nice way to stop the activity: we don't want
* the child to call TermHandler(). We
* don't care to check the outcome
* because we can be called from
* PeriodicChildDeath (pid
* non-existent). */
kill(activities[i].pid, SIGKILL);
}
/* remove traces of this activity */
UnregisterObject(activities[i].name);
/* remove the record */
for(j = i; j < (activityCount -1); j++) {
activities[j] = activities[j+1];
}
activities = REALLOC(activities, --activityCount * sizeof(PeriodicActivity));
if (activities == NULL && activityCount > 0) {
ABORT("StopPeriodicActivity: out of memory\n");
}
/* now there is one less activityCount and i
* points to the next activity: adjust */
i--;
/* we stopped an activity */
ret = 1;
}
}
if (registration[0] != '\0') {
ReleaseNWSLock(&lock);
}
return ret;
}
|