]>
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
)
40 int Adaptation::History::recordXactStart(const String
&sid
, const timeval
&when
, bool retrying
)
43 Must(!theEntries
.empty()); // or there would be nothing to retry
44 theEntries
.back().retried
= true;
46 theEntries
.push_back(Adaptation::History::Entry(sid
, when
));
47 return theEntries
.size() - 1; // record position becomes history ID
50 void Adaptation::History::recordXactFinish(int hid
)
52 Must(0 <= hid
&& hid
< static_cast<int>(theEntries
.size()));
53 theEntries
[hid
].stop();
56 void Adaptation::History::allLogString(const char *serviceId
, String
&s
)
59 bool prevWasRetried
= false;
60 // XXX: Fix Vector<> so that we can use const_iterator here
61 typedef Adaptation::History::Entries::iterator ECI
;
62 for (ECI i
= theEntries
.begin(); i
!= theEntries
.end(); ++i
) {
63 // TODO: here and below, optimize service ID comparison?
64 if (!serviceId
|| i
->service
== serviceId
) {
65 if (s
.size() > 0) // not the first logged time, must delimit
66 s
.append(prevWasRetried
? "+" : ",");
69 snprintf(buf
, sizeof(buf
), "%d", i
->rptm());
72 // continue; we may have two identical services (e.g., for retries)
74 prevWasRetried
= i
->retried
;
78 void Adaptation::History::sumLogString(const char *serviceId
, String
&s
)
81 int retriedRptm
= 0; // sum of rptm times of retried transactions
82 typedef Adaptation::History::Entries::iterator ECI
;
83 for (ECI i
= theEntries
.begin(); i
!= theEntries
.end(); ++i
) {
84 if (i
->retried
) { // do not log retried xact but accumulate their time
85 retriedRptm
+= i
->rptm();
86 } else 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