]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/adaptation/History.cc
3 #include "TextException.h"
5 #include "HttpRequest.h" /* for alLogformatHasAdaptToken */
6 #include "adaptation/Config.h"
7 #include "adaptation/History.h"
9 /// impossible services value to identify unset theNextServices
10 const static char *TheNullServices
= ",null,";
12 Adaptation::History::Entry::Entry(const String
&sid
, const timeval
&when
):
13 service(sid
), start(when
), theRptm(-1), retried(false)
17 Adaptation::History::Entry::Entry():
18 start(current_time
), theRptm(-1), retried(false)
22 void Adaptation::History::Entry::stop()
24 // theRptm may already be set if the access log entry has already been made
25 (void)rptm(); // will cache result in theRptm if not set already
28 int Adaptation::History::Entry::rptm()
31 theRptm
= tvSubMsec(start
, current_time
);
36 Adaptation::History::History(): theNextServices(TheNullServices
) {
39 int Adaptation::History::recordXactStart(const String
&sid
, const timeval
&when
, bool retrying
)
42 Must(!theEntries
.empty()); // or there would be nothing to retry
43 theEntries
.back().retried
= true;
45 theEntries
.push_back(Adaptation::History::Entry(sid
, when
));
46 return theEntries
.size() - 1; // record position becomes history ID
49 void Adaptation::History::recordXactFinish(int hid
)
51 Must(0 <= hid
&& hid
< static_cast<int>(theEntries
.size()));
52 theEntries
[hid
].stop();
55 void Adaptation::History::allLogString(const char *serviceId
, String
&s
)
58 bool prevWasRetried
= false;
59 // XXX: Fix Vector<> so that we can use const_iterator here
60 typedef Adaptation::History::Entries::iterator ECI
;
61 for (ECI i
= theEntries
.begin(); i
!= theEntries
.end(); ++i
) {
62 // TODO: here and below, optimize service ID comparison?
63 if (!serviceId
|| i
->service
== serviceId
) {
64 if (s
.size() > 0) // not the first logged time, must delimit
65 s
.append(prevWasRetried
? "+" : ",");
68 snprintf(buf
, sizeof(buf
), "%d", i
->rptm());
71 // continue; we may have two identical services (e.g., for retries)
73 prevWasRetried
= i
->retried
;
77 void Adaptation::History::sumLogString(const char *serviceId
, String
&s
)
80 int retriedRptm
= 0; // sum of rptm times of retried transactions
81 typedef Adaptation::History::Entries::iterator ECI
;
82 for (ECI i
= theEntries
.begin(); i
!= theEntries
.end(); ++i
) {
83 if (i
->retried
) { // do not log retried xact but accumulate their time
84 retriedRptm
+= i
->rptm();
86 if (!serviceId
|| i
->service
== serviceId
) {
87 if (s
.size() > 0) // not the first logged time, must delimit
91 snprintf(buf
, sizeof(buf
), "%d", retriedRptm
+ i
->rptm());
94 // continue; we may have two identical services (e.g., for retries)
101 // the last transaction is never retried or it would not be the last
105 void Adaptation::History::updateXxRecord(const char *name
, const String
&value
)
111 bool Adaptation::History::getXxRecord(String
&name
, String
&value
) const
113 if (theXxName
.size() <= 0)
121 void Adaptation::History::updateNextServices(const String
&services
)
123 if (theNextServices
!= TheNullServices
)
124 debugs(93,3, HERE
<< "old services: " << theNextServices
);
125 debugs(93,3, HERE
<< "new services: " << services
);
126 Must(services
!= TheNullServices
);
127 theNextServices
= services
;
130 bool Adaptation::History::extractNextServices(String
&value
)
132 if (theNextServices
== TheNullServices
)
135 value
= theNextServices
;
136 theNextServices
= TheNullServices
; // prevents resetting the plan twice