]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/adaptation/History.cc
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
10 #include "adaptation/Config.h"
11 #include "adaptation/History.h"
12 #include "base/TextException.h"
15 #include "SquidTime.h"
17 /// impossible services value to identify unset theNextServices
18 const static char *TheNullServices
= ",null,";
20 Adaptation::History::Entry::Entry(const String
&serviceId
, const timeval
&when
):
21 service(serviceId
), start(when
), theRptm(-1), retried(false)
25 Adaptation::History::Entry::Entry():
26 start(current_time
), theRptm(-1), retried(false)
30 void Adaptation::History::Entry::stop()
32 // theRptm may already be set if the access log entry has already been made
33 (void)rptm(); // will cache result in theRptm if not set already
36 int Adaptation::History::Entry::rptm()
39 theRptm
= tvSubMsec(start
, current_time
);
43 Adaptation::History::History():
46 theNextServices(TheNullServices
)
50 int Adaptation::History::recordXactStart(const String
&serviceId
, const timeval
&when
, bool retrying
)
52 // the history will be empty on retries if it was enabled after the failure
53 if (retrying
&& !theEntries
.empty())
54 theEntries
.back().retried
= true;
56 theEntries
.push_back(Adaptation::History::Entry(serviceId
, when
));
57 return theEntries
.size() - 1; // record position becomes history ID
60 void Adaptation::History::recordXactFinish(int hid
)
62 Must(0 <= hid
&& hid
< static_cast<int>(theEntries
.size()));
63 theEntries
[hid
].stop();
66 void Adaptation::History::allLogString(const char *serviceId
, String
&s
)
69 bool prevWasRetried
= false;
70 // XXX: Fix Vector<> so that we can use const_iterator here
71 typedef Adaptation::History::Entries::iterator ECI
;
72 for (ECI i
= theEntries
.begin(); i
!= theEntries
.end(); ++i
) {
73 // TODO: here and below, optimize service ID comparison?
74 if (!serviceId
|| i
->service
== serviceId
) {
75 if (s
.size() > 0) // not the first logged time, must delimit
76 s
.append(prevWasRetried
? "+" : ",");
79 snprintf(buf
, sizeof(buf
), "%d", i
->rptm());
82 // continue; we may have two identical services (e.g., for retries)
84 prevWasRetried
= i
->retried
;
88 void Adaptation::History::sumLogString(const char *serviceId
, String
&s
)
91 int retriedRptm
= 0; // sum of rptm times of retried transactions
92 typedef Adaptation::History::Entries::iterator ECI
;
93 for (ECI i
= theEntries
.begin(); i
!= theEntries
.end(); ++i
) {
94 if (i
->retried
) { // do not log retried xact but accumulate their time
95 retriedRptm
+= i
->rptm();
96 } else if (!serviceId
|| i
->service
== serviceId
) {
97 if (s
.size() > 0) // not the first logged time, must delimit
101 snprintf(buf
, sizeof(buf
), "%d", retriedRptm
+ i
->rptm());
104 // continue; we may have two identical services (e.g., for retries)
111 // the last transaction is never retried or it would not be the last
115 void Adaptation::History::updateXxRecord(const char *name
, const String
&value
)
121 bool Adaptation::History::getXxRecord(String
&name
, String
&value
) const
123 if (theXxName
.size() <= 0)
131 void Adaptation::History::updateNextServices(const String
&services
)
133 if (theNextServices
!= TheNullServices
)
134 debugs(93,3, HERE
<< "old services: " << theNextServices
);
135 debugs(93,3, HERE
<< "new services: " << services
);
136 Must(services
!= TheNullServices
);
137 theNextServices
= services
;
140 bool Adaptation::History::extractNextServices(String
&value
)
142 if (theNextServices
== TheNullServices
)
145 value
= theNextServices
;
146 theNextServices
= TheNullServices
; // prevents resetting the plan twice
150 void Adaptation::History::recordMeta(const HttpHeader
*lm
)
153 lastMeta
.update(lm
, NULL
);
155 allMeta
.update(lm
, NULL
);
160 Adaptation::History::recordAdaptationService(SBuf
&srvId
)
162 theAdaptationServices
.push_back(srvId
);
166 Adaptation::History::setFutureServices(const DynamicGroupCfg
&services
)
168 if (!theFutureServices
.empty())
169 debugs(93,3, HERE
<< "old future services: " << theFutureServices
);
170 debugs(93,3, HERE
<< "new future services: " << services
);
171 theFutureServices
= services
; // may be empty
174 bool Adaptation::History::extractFutureServices(DynamicGroupCfg
&value
)
176 if (theFutureServices
.empty())
179 value
= theFutureServices
;
180 theFutureServices
.clear();