Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-01-08 10:33:39

0001 #ifndef __XRDPFC_STATS_HH__
0002 #define __XRDPFC_STATS_HH__
0003 
0004 //----------------------------------------------------------------------------------
0005 // Copyright (c) 2014 by Board of Trustees of the Leland Stanford, Jr., University
0006 // Author: Alja Mrak-Tadel, Matevz Tadel, Brian Bockelman
0007 //----------------------------------------------------------------------------------
0008 // XRootD is free software: you can redistribute it and/or modify
0009 // it under the terms of the GNU Lesser General Public License as published by
0010 // the Free Software Foundation, either version 3 of the License, or
0011 // (at your option) any later version.
0012 //
0013 // XRootD is distributed in the hope that it will be useful,
0014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
0015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0016 // GNU General Public License for more details.
0017 //
0018 // You should have received a copy of the GNU Lesser General Public License
0019 // along with XRootD.  If not, see <http://www.gnu.org/licenses/>.
0020 //----------------------------------------------------------------------------------
0021 
0022 namespace XrdPfc
0023 {
0024 
0025 //----------------------------------------------------------------------------
0026 //! Statistics of cache utilisation by a File object.
0027 //  Used both as aggregation of usage by a single file as well as for
0028 //  collecting per-directory statistics on time-interval basis. In this second
0029 //  case they are used as "deltas" ... differences in respect to a previous
0030 //  reference value.
0031 //  For running averages / deltas, one might need a version with doubles, so
0032 //  it might make sense to template this. And add some timestamp.
0033 //----------------------------------------------------------------------------
0034 class Stats
0035 {
0036 public:
0037    int       m_NumIos = 0;          //!< number of IO objects attached during this access
0038    int       m_Duration = 0;        //!< total duration of all IOs attached
0039    long long m_BytesHit = 0;        //!< number of bytes served from disk
0040    long long m_BytesMissed = 0;     //!< number of bytes served from remote and cached
0041    long long m_BytesBypassed = 0;   //!< number of bytes served directly through XrdCl
0042    long long m_BytesWritten = 0;    //!< number of bytes written to disk
0043    long long m_StBlocksAdded = 0;   //!< number of 512-byte blocks the file has grown by
0044    int       m_NCksumErrors = 0;    //!< number of checksum errors while getting data from remote
0045 
0046    //----------------------------------------------------------------------
0047 
0048    Stats() = default;
0049 
0050    Stats(const Stats& s) = default;
0051 
0052    Stats& operator=(const Stats&) = default;
0053 
0054    Stats(const Stats& a, const Stats& b) :
0055       m_NumIos        (a.m_NumIos       + b.m_NumIos),
0056       m_Duration      (a.m_Duration      + b.m_Duration),
0057       m_BytesHit      (a.m_BytesHit      + b.m_BytesHit),
0058       m_BytesMissed   (a.m_BytesMissed   + b.m_BytesMissed),
0059       m_BytesBypassed (a.m_BytesBypassed + b.m_BytesBypassed),
0060       m_BytesWritten  (a.m_BytesWritten  + b.m_BytesWritten),
0061       m_StBlocksAdded (a.m_StBlocksAdded + b.m_StBlocksAdded),
0062       m_NCksumErrors  (a.m_NCksumErrors  + b.m_NCksumErrors)
0063    {}
0064 
0065    //----------------------------------------------------------------------
0066 
0067    void AddReadStats(const Stats &s)
0068    {
0069       m_BytesHit      += s.m_BytesHit;
0070       m_BytesMissed   += s.m_BytesMissed;
0071       m_BytesBypassed += s.m_BytesBypassed;
0072    }
0073 
0074    void AddBytesHit(long long bh)
0075    {
0076       m_BytesHit      += bh;
0077    }
0078 
0079    void AddWriteStats(long long bytes_written, int n_cks_errs)
0080    {
0081       m_BytesWritten += bytes_written;
0082       m_NCksumErrors += n_cks_errs;
0083    }
0084 
0085    void IoAttach()
0086    {
0087       ++m_NumIos;
0088    }
0089 
0090    void IoDetach(int duration)
0091    {
0092       m_Duration += duration;
0093    }
0094 
0095    //----------------------------------------------------------------------
0096 
0097    long long BytesRead() const
0098    {
0099       return m_BytesHit + m_BytesMissed + m_BytesBypassed;
0100    }
0101 
0102    long long BytesReadAndWritten() const
0103    {
0104       return BytesRead() + m_BytesWritten;
0105    }
0106 
0107    void DeltaToReference(const Stats& ref)
0108    {
0109       m_NumIos        = ref.m_NumIos        - m_NumIos;
0110       m_Duration      = ref.m_Duration      - m_Duration;
0111       m_BytesHit      = ref.m_BytesHit      - m_BytesHit;
0112       m_BytesMissed   = ref.m_BytesMissed   - m_BytesMissed;
0113       m_BytesBypassed = ref.m_BytesBypassed - m_BytesBypassed;
0114       m_BytesWritten  = ref.m_BytesWritten  - m_BytesWritten;
0115       m_StBlocksAdded = ref.m_StBlocksAdded - m_StBlocksAdded;
0116       m_NCksumErrors  = ref.m_NCksumErrors  - m_NCksumErrors;
0117    }
0118 
0119    void AddUp(const Stats& s)
0120    {
0121       m_NumIos        += s.m_NumIos;
0122       m_Duration      += s.m_Duration;
0123       m_BytesHit      += s.m_BytesHit;
0124       m_BytesMissed   += s.m_BytesMissed;
0125       m_BytesBypassed += s.m_BytesBypassed;
0126       m_BytesWritten  += s.m_BytesWritten;
0127       m_StBlocksAdded += s.m_StBlocksAdded;
0128       m_NCksumErrors  += s.m_NCksumErrors;
0129    }
0130 
0131    void Reset()
0132    {
0133       m_NumIos        = 0;
0134       m_Duration      = 0;
0135       m_BytesHit      = 0;
0136       m_BytesMissed   = 0;
0137       m_BytesBypassed = 0;
0138       m_BytesWritten  = 0;
0139       m_StBlocksAdded = 0;
0140       m_NCksumErrors  = 0;
0141    }
0142 };
0143 
0144 //==============================================================================
0145 
0146 class DirStats : public Stats
0147 {
0148 public:
0149    long long m_StBlocksRemoved = 0; // number of 512-byte blocks removed from the directory
0150    int       m_NFilesOpened = 0;
0151    int       m_NFilesClosed = 0;
0152    int       m_NFilesCreated = 0;
0153    int       m_NFilesRemoved = 0; // purged or otherwise (error, direct requests)
0154    int       m_NDirectoriesCreated = 0;
0155    int       m_NDirectoriesRemoved = 0;
0156 
0157    //----------------------------------------------------------------------
0158 
0159    DirStats() = default;
0160 
0161    DirStats(const DirStats& s) = default;
0162 
0163    DirStats& operator=(const DirStats&) = default;
0164 
0165    DirStats(const DirStats& a, const DirStats& b) :
0166       Stats(a, b),
0167       m_StBlocksRemoved     (a.m_StBlocksRemoved     + b.m_StBlocksRemoved),
0168       m_NFilesOpened        (a.m_NFilesOpened        + b.m_NFilesOpened),
0169       m_NFilesClosed        (a.m_NFilesClosed        + b.m_NFilesClosed),
0170       m_NFilesCreated       (a.m_NFilesCreated       + b.m_NFilesCreated),
0171       m_NFilesRemoved       (a.m_NFilesRemoved       + b.m_NFilesRemoved),
0172       m_NDirectoriesCreated (a.m_NDirectoriesCreated + b.m_NDirectoriesCreated),
0173       m_NDirectoriesRemoved (a.m_NDirectoriesRemoved + b.m_NDirectoriesRemoved)
0174    {}
0175 
0176    //----------------------------------------------------------------------
0177 
0178    using Stats::DeltaToReference; // activate overload based on arg
0179    void DeltaToReference(const DirStats& ref)
0180    {
0181       Stats::DeltaToReference(ref);
0182       m_StBlocksRemoved     = ref.m_StBlocksRemoved     - m_StBlocksRemoved;
0183       m_NFilesOpened        = ref.m_NFilesOpened        - m_NFilesOpened;
0184       m_NFilesClosed        = ref.m_NFilesClosed        - m_NFilesClosed;
0185       m_NFilesCreated       = ref.m_NFilesCreated       - m_NFilesCreated;
0186       m_NFilesRemoved       = ref.m_NFilesRemoved       - m_NFilesRemoved;
0187       m_NDirectoriesCreated = ref.m_NDirectoriesCreated - m_NDirectoriesCreated;
0188       m_NDirectoriesRemoved = ref.m_NDirectoriesRemoved - m_NDirectoriesRemoved;
0189    }
0190 
0191    using Stats::AddUp; // activate overload based on arg
0192    void AddUp(const DirStats& s)
0193    {
0194       Stats::AddUp(s);
0195       m_StBlocksRemoved     += s.m_StBlocksRemoved;
0196       m_NFilesOpened        += s.m_NFilesOpened;
0197       m_NFilesClosed        += s.m_NFilesClosed;
0198       m_NFilesCreated       += s.m_NFilesCreated;
0199       m_NFilesRemoved       += s.m_NFilesRemoved;
0200       m_NDirectoriesCreated += s.m_NDirectoriesCreated;
0201       m_NDirectoriesRemoved += s.m_NDirectoriesRemoved;
0202    }
0203 
0204    using Stats::Reset; // activate overload based on arg
0205    void Reset()
0206    {
0207       Stats::Reset();
0208       m_StBlocksRemoved     = 0;
0209       m_NFilesOpened        = 0;
0210       m_NFilesClosed        = 0;
0211       m_NFilesCreated       = 0;
0212       m_NFilesRemoved       = 0;
0213       m_NDirectoriesCreated = 0;
0214       m_NDirectoriesRemoved = 0;
0215    }
0216 };
0217 
0218 }
0219 
0220 #endif