]>
Commit | Line | Data |
---|---|---|
3ff65596 AR |
1 | #include "config.h" |
2 | #include "globals.h" | |
3 | #include "TextException.h" | |
4 | #include "SquidTime.h" | |
5 | #include "HttpRequest.h" /* for alLogformatHasAdaptToken */ | |
6 | #include "adaptation/Config.h" | |
7 | #include "adaptation/History.h" | |
8 | ||
a22e6cd3 | 9 | /// impossible services value to identify unset theNextServices |
8536ad02 | 10 | const static char *TheNullServices = ",null,"; |
a22e6cd3 | 11 | |
b0365bd9 FC |
12 | Adaptation::History::Entry::Entry(const String &serviceId, const timeval &when): |
13 | service(serviceId), start(when), theRptm(-1), retried(false) | |
3ff65596 AR |
14 | { |
15 | } | |
16 | ||
17 | Adaptation::History::Entry::Entry(): | |
e1381638 | 18 | start(current_time), theRptm(-1), retried(false) |
3ff65596 AR |
19 | { |
20 | } | |
21 | ||
22 | void Adaptation::History::Entry::stop() | |
23 | { | |
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 | |
26 | } | |
27 | ||
28 | int Adaptation::History::Entry::rptm() | |
29 | { | |
30 | if (theRptm < 0) | |
31 | theRptm = tvSubMsec(start, current_time); | |
32 | return theRptm; | |
33 | } | |
34 | ||
35 | ||
e1381638 AJ |
36 | Adaptation::History::History(): theNextServices(TheNullServices) |
37 | { | |
a22e6cd3 AR |
38 | } |
39 | ||
b0365bd9 | 40 | int Adaptation::History::recordXactStart(const String &serviceId, const timeval &when, bool retrying) |
3ff65596 AR |
41 | { |
42 | if (retrying) { | |
43 | Must(!theEntries.empty()); // or there would be nothing to retry | |
44 | theEntries.back().retried = true; | |
45 | } | |
b0365bd9 | 46 | theEntries.push_back(Adaptation::History::Entry(serviceId, when)); |
3ff65596 AR |
47 | return theEntries.size() - 1; // record position becomes history ID |
48 | } | |
49 | ||
50 | void Adaptation::History::recordXactFinish(int hid) | |
51 | { | |
52 | Must(0 <= hid && hid < static_cast<int>(theEntries.size())); | |
53 | theEntries[hid].stop(); | |
54 | } | |
55 | ||
56 | void Adaptation::History::allLogString(const char *serviceId, String &s) | |
57 | { | |
58 | 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 ? "+" : ","); | |
67 | ||
68 | char buf[64]; | |
69 | snprintf(buf, sizeof(buf), "%d", i->rptm()); | |
70 | s.append(buf); | |
71 | ||
72 | // continue; we may have two identical services (e.g., for retries) | |
73 | } | |
74 | prevWasRetried = i->retried; | |
75 | } | |
76 | } | |
77 | ||
78 | void Adaptation::History::sumLogString(const char *serviceId, String &s) | |
79 | { | |
80 | 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(); | |
e1381638 | 86 | } else if (!serviceId || i->service == serviceId) { |
3ff65596 AR |
87 | if (s.size() > 0) // not the first logged time, must delimit |
88 | s.append(","); | |
89 | ||
90 | char buf[64]; | |
91 | snprintf(buf, sizeof(buf), "%d", retriedRptm + i->rptm()); | |
92 | s.append(buf); | |
93 | ||
94 | // continue; we may have two identical services (e.g., for retries) | |
95 | } | |
96 | ||
97 | if (!i->retried) | |
98 | retriedRptm = 0; | |
99 | } | |
100 | ||
101 | // the last transaction is never retried or it would not be the last | |
e1381638 | 102 | Must(!retriedRptm); |
3ff65596 AR |
103 | } |
104 | ||
105 | void Adaptation::History::updateXxRecord(const char *name, const String &value) | |
106 | { | |
107 | theXxName = name; | |
108 | theXxValue = value; | |
109 | } | |
110 | ||
111 | bool Adaptation::History::getXxRecord(String &name, String &value) const | |
112 | { | |
113 | if (theXxName.size() <= 0) | |
e1381638 | 114 | return false; |
3ff65596 AR |
115 | |
116 | name = theXxName; | |
117 | value = theXxValue; | |
118 | return true; | |
119 | } | |
120 | ||
a22e6cd3 | 121 | void Adaptation::History::updateNextServices(const String &services) |
3ff65596 | 122 | { |
1fcb2113 | 123 | if (theNextServices != TheNullServices) |
e1381638 | 124 | debugs(93,3, HERE << "old services: " << theNextServices); |
a22e6cd3 | 125 | debugs(93,3, HERE << "new services: " << services); |
1fcb2113 | 126 | Must(services != TheNullServices); |
a22e6cd3 AR |
127 | theNextServices = services; |
128 | } | |
3ff65596 | 129 | |
a22e6cd3 AR |
130 | bool Adaptation::History::extractNextServices(String &value) |
131 | { | |
1fcb2113 | 132 | if (theNextServices == TheNullServices) |
e1381638 | 133 | return false; |
3ff65596 | 134 | |
a22e6cd3 | 135 | value = theNextServices; |
1fcb2113 | 136 | theNextServices = TheNullServices; // prevents resetting the plan twice |
a22e6cd3 | 137 | return true; |
3ff65596 | 138 | } |