1 /*
2 BSD 3-Clause License
3 
4 Copyright (c) 2016-2020, Intel Corporation
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 * Redistributions of source code must retain the above copyright notice, this
11   list of conditions and the following disclaimer.
12 
13 * Redistributions in binary form must reproduce the above copyright notice,
14   this list of conditions and the following disclaimer in the documentation
15   and/or other materials provided with the distribution.
16 
17 * Neither the name of the copyright holder nor the names of its
18   contributors may be used to endorse or promote products derived from
19   this software without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 
33 #include "cpucounters.h"
34 #include "topology.h"
35 
36 namespace pcm {
37 
uncoreCounterState(void) const38 UncoreCounterState ServerUncore::uncoreCounterState( void ) const
39 {
40     UncoreCounterState ucs;
41     // Fill the ucs
42     PCM* pcm = PCM::getInstance();
43     pcm->readAndAggregateUncoreMCCounters( socketID(), ucs );
44     pcm->readAndAggregateEnergyCounters( socketID(), ucs );
45     pcm->readAndAggregatePackageCStateResidencies( refCore()->msrHandle(), ucs );
46 
47     return ucs;
48 }
49 
Socket(PCM * m,int32 apicID,int32 logicalID)50 Socket::Socket( PCM* m, int32 apicID, int32 logicalID )
51     : pcm_(m), refCore_(nullptr), apicID_(apicID), logicalID_(logicalID)
52 {
53     if ( pcm_->isServerCPU() )
54         uncore_ = new ServerUncore( pcm_, logicalID );
55     else if ( pcm_->isClientCPU() )
56         uncore_ = new ClientUncore( pcm_, logicalID );
57     else
58         throw std::runtime_error( "ERROR: Neither a client nor a server part, please fix the code!" );
59 }
60 
socketCounterState(void) const61 SocketCounterState Socket::socketCounterState( void ) const {
62     SocketCounterState scs;
63     // Fill the scs
64     // by iterating the cores
65     for( auto core : cores_ ) {
66         scs.BasicCounterState::operator += ( core->coreCounterState() );
67     }
68     // and the uncore
69     scs.UncoreCounterState::operator += ( uncore_->uncoreCounterState() );
70     PCM::getInstance()->readPackageThermalHeadroom( socketID(), scs );
71     return scs;
72 }
73 
dispatch(SystemRoot const & syp)74 void Aggregator::dispatch( SystemRoot const& syp ) {
75     // std::cerr << "Aggregator::dispatch( SystemRoot )\n";
76     dispatchedAt_ = std::chrono::steady_clock::now();
77     // CoreCounterStates are fetched asynchronously here
78     for ( auto* socket : syp.sockets() )
79         socket->accept( *this );
80     // Dispatching offlined cores
81     for ( auto* htp : syp.offlinedThreadsAtStart() )
82         htp->accept( *this );
83 
84     auto ccsFuturesIter = ccsFutures_.begin();
85     auto ccsIter = ccsVector_.begin();
86     // int i;
87     // i = 0;
88     for ( ; ccsFuturesIter != ccsFutures_.end() && ccsIter != ccsVector_.end(); ++ccsFuturesIter, ++ccsIter ) {
89         // std::cerr << "Works ccsFuture: " << ++i << "\n";
90         (*ccsIter) = (*ccsFuturesIter).get();
91     }
92 
93     // Aggregate BasicCounterStates
94     for ( auto* socket : syp.sockets() ) {
95         for ( auto* core : socket->cores() )
96             for ( auto* thread : core->threads() )
97                 socsVector_[ socket->socketID() ] += ( ccsVector_[ thread->osID() ] );
98         // UncoreCounterStates have not been filled here so it is ok to add
99         // the entire SocketCounterState here
100         sycs_ += socsVector_[ socket->socketID() ];
101     }
102 
103     // Fetch and aggregate UncoreCounterStates
104     auto ucsFuturesIter = ucsFutures_.begin();
105     auto socsIter = socsVector_.begin();
106     // i = 0;
107     for ( ; ucsFuturesIter != ucsFutures_.end() && socsIter != socsVector_.end(); ++ucsFuturesIter, ++socsIter ) {
108         // std::cerr << "Works ucsFuture: " << ++i << "\n";
109         // Because we already aggregated the Basic/CoreCounterStates above, sycs_
110         // only needs the ucs added here. If we would add socs to sycs we would
111         // count all Basic/CoreCounterState counters double
112         UncoreCounterState ucs = (*ucsFuturesIter).get();
113         sycs_ += ucs;
114         (*socsIter) = std::move( ucs );
115     }
116     PCM* pcm = PCM::getInstance();
117     pcm->readQPICounters( sycs_ );
118 }
119 
Aggregator()120 Aggregator::Aggregator()
121 {
122     PCM* const pcm = PCM::getInstance();
123     // Resize user provided vectors to the right size
124     ccsVector_.resize( pcm->getNumCores() );
125     socsVector_.resize( pcm->getNumSockets() );
126     // Internal use only, need to be the same size as the user provided vectors
127     ccsFutures_.resize( pcm->getNumCores() );
128     ucsFutures_.resize( pcm->getNumSockets() );
129 }
130 
131 }// namespace pcm