1 /*
2  * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
3  *
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.
7  */
8 
9 #include "squid.h"
10 #include "base/PackableStream.h"
11 #include "ipc/Messages.h"
12 #include "ipc/TypedMsgHdr.h"
13 #include "mgr/Registration.h"
14 #include "sbuf/DetailedStats.h"
15 #include "SBufStatsAction.h"
16 
SBufStatsAction(const Mgr::CommandPointer & cmd_)17 SBufStatsAction::SBufStatsAction(const Mgr::CommandPointer &cmd_):
18     Action(cmd_)
19 { } //default constructor is OK for data member
20 
21 SBufStatsAction::Pointer
Create(const Mgr::CommandPointer & cmd)22 SBufStatsAction::Create(const Mgr::CommandPointer &cmd)
23 {
24     return new SBufStatsAction(cmd);
25 }
26 
27 void
add(const Mgr::Action & action)28 SBufStatsAction::add(const Mgr::Action& action)
29 {
30     sbdata += dynamic_cast<const SBufStatsAction&>(action).sbdata;
31     mbdata += dynamic_cast<const SBufStatsAction&>(action).mbdata;
32     sbsizesatdestruct += dynamic_cast<const SBufStatsAction&>(action).sbsizesatdestruct;
33     mbsizesatdestruct += dynamic_cast<const SBufStatsAction&>(action).mbsizesatdestruct;
34 }
35 
36 void
collect()37 SBufStatsAction::collect()
38 {
39     sbdata = SBuf::GetStats();
40     mbdata = MemBlob::GetStats();
41     sbsizesatdestruct = collectSBufDestructTimeStats();
42     mbsizesatdestruct = collectMemBlobDestructTimeStats();
43 }
44 
45 static void
statHistSBufDumper(StoreEntry * sentry,int,double val,double size,int count)46 statHistSBufDumper(StoreEntry * sentry, int, double val, double size, int count)
47 {
48     if (count == 0)
49         return;
50     storeAppendPrintf(sentry, "\t%d-%d\t%d\n", static_cast<int>(val), static_cast<int>(val+size), count);
51 }
52 
53 void
dump(StoreEntry * entry)54 SBufStatsAction::dump(StoreEntry* entry)
55 {
56     PackableStream ses(*entry);
57     ses << "\n\n\nThese statistics are experimental; their format and contents "
58         "should not be relied upon, they are bound to change as "
59         "the SBuf feature is evolved\n";
60     sbdata.dump(ses);
61     mbdata.dump(ses);
62     ses << "\n";
63     ses << "SBuf size distribution at destruct time:\n";
64     sbsizesatdestruct.dump(entry,statHistSBufDumper);
65     ses << "MemBlob capacity distribution at destruct time:\n";
66     mbsizesatdestruct.dump(entry,statHistSBufDumper);
67 }
68 
69 void
pack(Ipc::TypedMsgHdr & msg) const70 SBufStatsAction::pack(Ipc::TypedMsgHdr& msg) const
71 {
72     msg.setType(Ipc::mtCacheMgrResponse);
73     msg.putPod(sbdata);
74     msg.putPod(mbdata);
75 }
76 
77 void
unpack(const Ipc::TypedMsgHdr & msg)78 SBufStatsAction::unpack(const Ipc::TypedMsgHdr& msg)
79 {
80     msg.checkType(Ipc::mtCacheMgrResponse);
81     msg.getPod(sbdata);
82     msg.getPod(mbdata);
83 }
84 
85 void
RegisterWithCacheManager()86 SBufStatsAction::RegisterWithCacheManager()
87 {
88     Mgr::RegisterAction("sbuf", "String-Buffer statistics", &SBufStatsAction::Create, 0 , 1);
89 }
90 
91