]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/adaptation/History.cc
2 #include "adaptation/Config.h"
3 #include "adaptation/History.h"
4 #include "base/TextException.h"
6 #include "HttpRequest.h" /* for alLogformatHasAdaptToken */
9 /// impossible services value to identify unset theNextServices
10 const static char *TheNullServices
= ",null,";
12 Adaptation::History::Entry::Entry(const String
&serviceId
, const timeval
&when
):
13 service(serviceId
), 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():
39 theNextServices(TheNullServices
)
43 int Adaptation::History::recordXactStart(const String
&serviceId
, const timeval
&when
, bool retrying
)
45 // the history will be empty on retries if it was enabled after the failure
46 if (retrying
&& !theEntries
.empty())
47 theEntries
.back().retried
= true;
49 theEntries
.push_back(Adaptation::History::Entry(serviceId
, when
));
50 return theEntries
.size() - 1; // record position becomes history ID
53 void Adaptation::History::recordXactFinish(int hid
)
55 Must(0 <= hid
&& hid
< static_cast<int>(theEntries
.size()));
56 theEntries
[hid
].stop();
59 void Adaptation::History::allLogString(const char *serviceId
, String
&s
)
62 bool prevWasRetried
= false;
63 // XXX: Fix Vector<> so that we can use const_iterator here
64 typedef Adaptation::History::Entries::iterator ECI
;
65 for (ECI i
= theEntries
.begin(); i
!= theEntries
.end(); ++i
) {
66 // TODO: here and below, optimize service ID comparison?
67 if (!serviceId
|| i
->service
== serviceId
) {
68 if (s
.size() > 0) // not the first logged time, must delimit
69 s
.append(prevWasRetried
? "+" : ",");
72 snprintf(buf
, sizeof(buf
), "%d", i
->rptm());
75 // continue; we may have two identical services (e.g., for retries)
77 prevWasRetried
= i
->retried
;
81 void Adaptation::History::sumLogString(const char *serviceId
, String
&s
)
84 int retriedRptm
= 0; // sum of rptm times of retried transactions
85 typedef Adaptation::History::Entries::iterator ECI
;
86 for (ECI i
= theEntries
.begin(); i
!= theEntries
.end(); ++i
) {
87 if (i
->retried
) { // do not log retried xact but accumulate their time
88 retriedRptm
+= i
->rptm();
89 } else if (!serviceId
|| i
->service
== serviceId
) {
90 if (s
.size() > 0) // not the first logged time, must delimit
94 snprintf(buf
, sizeof(buf
), "%d", retriedRptm
+ i
->rptm());
97 // continue; we may have two identical services (e.g., for retries)
104 // the last transaction is never retried or it would not be the last
108 void Adaptation::History::updateXxRecord(const char *name
, const String
&value
)
114 bool Adaptation::History::getXxRecord(String
&name
, String
&value
) const
116 if (theXxName
.size() <= 0)
124 void Adaptation::History::updateNextServices(const String
&services
)
126 if (theNextServices
!= TheNullServices
)
127 debugs(93,3, HERE
<< "old services: " << theNextServices
);
128 debugs(93,3, HERE
<< "new services: " << services
);
129 Must(services
!= TheNullServices
);
130 theNextServices
= services
;
133 bool Adaptation::History::extractNextServices(String
&value
)
135 if (theNextServices
== TheNullServices
)
138 value
= theNextServices
;
139 theNextServices
= TheNullServices
; // prevents resetting the plan twice
143 void Adaptation::History::recordMeta(const HttpHeader
*lm
)
146 lastMeta
.update(lm
, NULL
);
148 allMeta
.update(lm
, NULL
);