31 static int CheckUser(
const char *, XrdProofGroup *g,
void *u)
33 const char *usr = (
const char *)u;
35 if (g && usr && g->HasMember(usr))
46 static int ExportGroup(
const char *, XrdProofGroup *g,
void *u)
48 XrdOucString *msg = (XrdOucString *)u;
50 if (msg->length() > 0)
53 *msg = g->Name(); *msg +=
": ";
56 *msg +=
", members(s): ";
65 static int PrintGroup(
const char *, XrdProofGroup *g,
void *)
76 static int AuxFunc(
const char *, XrdProofGroup *g,
void *s)
78 XrdOucString *opt = (XrdOucString *)s;
80 if (!opt || opt->length() <= 0 || (*opt) ==
"getfirst")
84 if (opt->beginswith(
"getnextgrp:")) {
85 XrdOucString grp(
"||");
86 grp.insert(g->Name(),1);
87 if (opt->find(grp) == STR_NPOS) {
100 XrdProofGroup::XrdProofGroup(
const char *n,
const char *m)
101 : fName(n), fMembers(m)
107 fMutex =
new XrdSysRecMutex;
112 XrdProofGroup::~XrdProofGroup()
122 void XrdProofGroup::Print()
124 XPDLOC(GMGR,
"Group::Print")
126 XrdSysMutexHelper mhp(fMutex);
128 if (fName != "default") {
129 TRACE(ALL,
"+++ Group: "<<fName<<
", size "<<fSize<<
" member(s) ("<<fMembers<<
")");
130 TRACE(ALL,
"+++ Priority: "<<fPriority<<
", fraction: "<<fFraction);
131 TRACE(ALL,
"+++ End of Group: "<<fName);
133 TRACE(ALL,
"+++ Group: "<<fName);
134 TRACE(ALL,
"+++ Priority: "<<fPriority<<
", fraction: "<<fFraction);
135 TRACE(ALL,
"+++ End of Group: "<<fName);
142 void XrdProofGroup::Count(
const char *usr,
int n)
145 if (!usr || !usr[0] || n == 0)
148 XrdSysMutexHelper mhp(fMutex);
150 XrdProofGroupMember *m = fActives.Find(usr);
153 m =
new XrdProofGroupMember(usr);
154 fActives.Add(usr, m);
161 if (m->Active() <= 0) {
172 int XrdProofGroup::Active(
const char *usr)
174 XrdSysMutexHelper mhp(fMutex);
177 if (!usr || !usr[0]) {
180 XrdProofGroupMember *m = fActives.Find(usr);
181 if (m) na = m->Active();
190 bool XrdProofGroup::HasMember(
const char *usr)
192 XrdSysMutexHelper mhp(fMutex);
193 XrdOucString u(usr); u +=
",";
194 int iu = fMembers.find(u);
196 if (iu == 0 || fMembers[iu-1] ==
',')
204 XrdProofGroupMgr::XrdProofGroupMgr(
const char *fn)
218 XrdProofGroup *XrdProofGroupMgr::Apply(
int (*f)(
const char *, XrdProofGroup *,
221 return (fGroups.Num() > 0 ? fGroups.Apply(f,arg) : (XrdProofGroup *)0);
227 XrdOucString XrdProofGroupMgr::Export(
const char *grp)
229 XrdSysMutexHelper mhp(fMutex);
234 fGroups.Apply(ExportGroup, (
void *) &msg);
236 XrdProofGroup *g = fGroups.Find(grp);
237 ExportGroup(grp, g, (
void *) &msg);
246 void XrdProofGroupMgr::Print(
const char *grp)
248 XrdSysMutexHelper mhp(fMutex);
251 fGroups.Apply(PrintGroup, 0);
253 XrdProofGroup *g = fGroups.Find(grp);
254 PrintGroup(grp, g, 0);
264 XrdProofGroup *XrdProofGroupMgr::GetGroup(
const char *grp)
267 if (grp && strlen(grp) > 0) {
268 XrdSysMutexHelper mhp(fMutex);
269 return fGroups.Find(grp);
271 return (XrdProofGroup *)0;
281 XrdProofGroup *XrdProofGroupMgr::GetUserGroup(
const char *usr,
const char *grp)
283 XrdProofGroup *g = 0;
286 if (!usr || strlen(usr) <= 0)
289 XrdSysMutexHelper mhp(fMutex);
292 if (grp && strlen(grp) > 0) {
293 g = fGroups.Find(grp);
294 if (g && (!strncmp(g->Name(),
"default",7) || g->HasMember(usr)))
297 return (XrdProofGroup *)0;
301 g = fGroups.Apply(CheckUser, (
void *)usr);
304 return ((!g) ? fGroups.Find(
"default") : g);
316 XrdProofGroup *XrdProofGroupMgr::Next()
318 return fGroups.Apply(AuxFunc,&fIterator);
325 int XrdProofGroupMgr::Config(
const char *fn)
327 XPDLOC(GMGR,
"GroupMgr::Config")
329 if ((!fn || strlen(fn) <= 0)) {
330 if (fCfgFile.fName != fn) {
333 XrdSysMutexHelper mhp(fMutex);
337 fGroups.Add(
"default",
new XrdProofGroup(
"default"));
342 return fGroups.Num();
346 if (fCfgFile.fName != fn) {
348 XrdProofdAux::Expand(fCfgFile.fName);
354 if (stat(fCfgFile.fName.c_str(), &st) != 0)
356 TRACE(DBG,
"enter: time of last modification: " << st.st_mtime);
359 if (st.st_mtime <= fCfgFile.fMtime)
return fGroups.Num();
362 fCfgFile.fMtime = st.st_mtime;
365 XrdSysMutexHelper mhp(fMutex);
371 fGroups.Add(
"default",
new XrdProofGroup(
"default"));
375 if (ParseInfoFrom(fCfgFile.fName.c_str()) != 0) {
376 TRACE(XERR,
"problems parsing config file "<<fCfgFile.fName);
383 return fGroups.Num();
391 int XrdProofGroupMgr::ParseInfoFrom(
const char *fn)
393 XPDLOC(GMGR,
"GroupMgr::ParseInfoFrom")
396 if (!fn || strlen(fn) <= 0) {
397 TRACE(XERR,
"file name undefined!");
403 if (!(fin = fopen(fn,
"r"))) {
404 TRACE(XERR,
"cannot open file: "<<fn<<
" (errno:"<<errno<<
")");
410 while (fgets(lin,
sizeof(lin),fin)) {
412 if (lin[strlen(lin)-1] ==
'\n') lin[strlen(lin)-1] =
'\0';
414 if (lin[0] ==
'#' || strlen(lin) <= 0)
continue;
416 bool gotkey = 0, gotgrp = 0;
417 XrdOucString gl(lin), tok, key, group;
420 while ((from = gl.tokenize(tok, from,
',')) != -1) {
421 if (tok.length() > 0) {
425 }
else if (!gotgrp) {
433 if (!gotkey || !gotgrp) {
435 TRACE(DBG,
"incomplete line: " << lin);
439 if (key ==
"include") {
441 XrdOucString subfn = group;
443 XrdProofdAux::Expand(subfn);
445 if (ParseInfoFrom(subfn.c_str()) != 0) {
446 TRACE(XERR,
"problems parsing included file "<<subfn);
451 if (key ==
"priorityfile") {
453 fPriorityFile.fName = group;
454 XrdProofdAux::Expand(fPriorityFile.fName);
455 fPriorityFile.fMtime = 0;
460 XrdProofGroup *g = fGroups.Find(group.c_str());
463 if (key ==
"group") {
466 fGroups.Add(group.c_str(), (g =
new XrdProofGroup(group.c_str())));
467 while ((from = gl.tokenize(tok, from,
',')) != -1) {
468 if (tok.length() > 0)
470 g->AddMember(tok.c_str());
472 }
else if (key ==
"property") {
477 bool gotname = 0, gotnom = 0;
478 while ((from = gl.tokenize(tok, from,
',')) != -1) {
479 if (tok.length() > 0) {
483 }
else if (!gotnom) {
484 nom = atoi(tok.c_str());
490 if (!gotname || !gotnom) {
492 TRACE(DBG,
"incomplete property line: " << lin);
497 fGroups.Add(group.c_str(), (g =
new XrdProofGroup(group.c_str())));
498 if (name ==
"priority")
499 g->SetPriority((
float)nom);
500 if (name ==
"fraction")
515 int XrdProofGroupMgr::ReadPriorities()
517 XPDLOC(GMGR,
"GroupMgr::ReadPriorities")
521 if (stat(fPriorityFile.fName.c_str(), &st) != 0)
523 TRACE(DBG, "time of last modification: " << st.st_mtime);
526 if (st.st_mtime <= fPriorityFile.fMtime) {
527 TRACE(DBG,
"file unchanged since last reading - do nothing ");
532 fPriorityFile.fMtime = st.st_mtime;
536 if (!(fin = fopen(fPriorityFile.fName.c_str(),
"r"))) {
537 TRACE(XERR,
"cannot open file: "<<fPriorityFile.fName<<
" (errno:"<<errno<<
")");
542 XrdSysMutexHelper mhp(fMutex);
546 while (fgets(lin,
sizeof(lin),fin)) {
548 if (lin[strlen(lin)-1] ==
'\n') lin[strlen(lin)-1] =
'\0';
550 if (lin[0] ==
'#' || strlen(lin) <= 0)
continue;
552 XrdOucString gl(lin), group, value;
555 if ((from = gl.tokenize(group, 0,
'=')) == -1)
558 XrdProofGroup *g = fGroups.Find(group.c_str());
560 TRACE(XERR,
"found info for unknown group: "<<group<<
" - ignoring");
563 gl.tokenize(value, from,
'=');
564 if (value.length() <= 0) {
565 TRACE(XERR,
"value missing: read line is: '"<<gl<<
"'");
569 if (value.find(
'.') == STR_NPOS)
572 g->SetPriority((
float)strtod(value.c_str(),0));
585 static int GetGroupsInfo(
const char *, XrdProofGroup *g,
void *s)
587 XpdGroupGlobal_t *glo = (XpdGroupGlobal_t *)s;
590 if (g->Active() > 0) {
592 if (glo->prmin == -1 || g->Priority() < glo->prmin)
593 glo->prmin = g->Priority();
594 if (glo->prmax == -1 || g->Priority() > glo->prmax)
595 glo->prmax = g->Priority();
597 if (g->Fraction() > 0) {
598 g->SetFracEff((
float)(g->Fraction()));
599 glo->totfrac += (float)(g->Fraction());
616 static int SetGroupFracEff(
const char *, XrdProofGroup *g,
void *s)
618 XpdGroupEff_t *eff = (XpdGroupEff_t *)s;
620 if (eff && eff->glo) {
621 XpdGroupGlobal_t *glo = eff->glo;
622 if (g->Active() > 0) {
624 float ef = g->Priority() / glo->prmin;
626 }
else if (eff->opt == 1) {
627 if (g->Fraction() < 0) {
628 float ef = ((100. - glo->totfrac) / glo->nofrac);
631 }
else if (eff->opt == 2) {
632 if (g->FracEff() < 0) {
634 float ef = (eff->cut / glo->nofrac);
638 float ef = g->FracEff() * eff->norm;
659 int XrdProofGroupMgr::SetEffectiveFractions(
bool opri)
662 XpdGroupGlobal_t glo = {-1., -1., 0, 0.};
663 Apply(GetGroupsInfo, &glo);
665 XpdGroupEff_t eff = {0, &glo, 0.5, 1.};
670 Apply(SetGroupFracEff, &eff);
677 if (glo.totfrac < 100. && glo.nofrac > 0) {
679 Apply(SetGroupFracEff, &eff);
680 }
else if (glo.totfrac > 100) {
683 eff.norm = (glo.nofrac > 0) ? (100. - eff.cut)/glo.totfrac : 100./glo.totfrac ;
684 Apply(SetGroupFracEff, &eff);