CaliperSensorGroup.h 4.78 KB
Newer Older
1
2
3
//================================================================================
// Name        : CaliperSensorGroup.h
// Author      : Micha Mueller
Micha Müller's avatar
Micha Müller committed
4
// Contact     : info@dcdb.it
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Copyright   : Leibniz Supercomputing Centre
// Description : Header file for Caliper sensor group class.
//================================================================================

//================================================================================
// This file is part of DCDB (DataCenter DataBase)
// Copyright (C) 2019-2019 Leibniz Supercomputing Centre
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//================================================================================

#ifndef CALIPER_CALIPERSENSORGROUP_H_
#define CALIPER_CALIPERSENSORGROUP_H_

#include "../../includes/SensorGroupTemplate.h"

#include "CaliperSensorBase.h"

35
#include <atomic>
Micha Müller's avatar
Micha Müller committed
36
#include <semaphore.h>
37
38
#include <unordered_map>

39
40
41
42
43
44
/**
 * @brief SensorGroupTemplate specialization for this plugin.
 *
 * @ingroup caliper
 */
class CaliperSensorGroup : public SensorGroupTemplate<CaliperSensorBase> {
Micha Müller's avatar
Micha Müller committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

/*******************************************************************************
 * Keep in sync with DcdbPusher Caliper service
 ******************************************************************************/
#define SHM_MAX_RETRIES 5
#define MAX_SYMBOL_SIZE 512
#define MAX_PATH_SIZE  4096
#define MSGQ_SIZE      8192
#define STR_PREFIX     "/cali_dcdb_"
#define SHM_SIZE       (32*1024*1024)

typedef struct {
    uintptr_t pc;
    uint64_t  ts;
    unsigned  short cpu;
} snap_data;

/* Entry for an executable symbol in the symbol table */
typedef struct {
    uintptr_t start_addr;
    uintptr_t end_addr;
    char      name[MAX_SYMBOL_SIZE];
} fsym_data;

/* Defines a contiguous executable memory block */
typedef struct {
    uintptr_t start_addr;
    uintptr_t end_addr;
    uintptr_t offset; // offset as parsed from /proc//maps
    size_t    fsym_offset; // Offset in bytes from the address of this struct
                           // to the beginning of the associated symbol section
    size_t    fsym_count; // Number of symbols in this address range
    char      pathname[MAX_PATH_SIZE]; // Filepath + name of the binary where
                                       // this memory range comes from or
                                       // "[Anonymous]" if unknown
} addr_data;

/*
 * Layout of shared-memory file used to communicate with Caliper service:
 *
 * //Communication queue, aka ring buffer:
 * size_t r_index //points to the last read element
 * size_t w_index //points to the last written element
 * sem_t  r_sem;
 * sem_t  w_sem;
 * snap_data[MSGQ_SIZE]
 *
 * //symbol lookup data
 * size_t addr_count
 * addr_data[addr_count]
 * addr_count * (fsym_data[addr_data.fsym_count])
 */
static constexpr size_t lookup_data_offset = 2*sizeof(size_t)
                                           + 2*sizeof(sem_t)
                                           + MSGQ_SIZE*sizeof(snap_data);

101
102
public:
    CaliperSensorGroup(const std::string& name);
103
104
105
    CaliperSensorGroup(const CaliperSensorGroup& other);
    virtual ~CaliperSensorGroup();
    CaliperSensorGroup& operator=(const CaliperSensorGroup& other);
106

107
108
    bool execOnStart() final override;
    void execOnStop()  final override;
109

110
111
112
113
114
115
116
117
    std::vector<SBasePtr>& acquireSensors() final override {
        while (_lock.test_and_set(std::memory_order_acquire)) {}
        return _baseSensors;
    }

    void releaseSensors() final override {
        _lock.clear(std::memory_order_release);
    }
118
119
120
121

    void printGroupConfig(LOG_LEVEL ll) final override;

    void setGlobalMqttPrefix(const std::string& prefix) { _globalMqttPrefix = prefix; }
122

123
private:
124
    void read() final override;
125

126
127
    int _socket;
    int _connection;
Micha Müller's avatar
Micha Müller committed
128
129
130
    void*  _shm;
    int    _shmFile;
    size_t _shmFailCnt;
131
    std::string _globalMqttPrefix;
132

133
    std::atomic_flag _lock; ///< Lock to synchronize access to associated sensors
Micha Müller's avatar
Micha Müller committed
134
    //TODO aggregate sensor values: store pair of S_Ptr and counter. Only push values at end of iteration
135
    std::unordered_map<std::string, S_Ptr> _sensorIndex; ///< Additional sensor storage for fast lookup
136
137
138
};

#endif /* CALIPER_CALIPERSENSORGROUP_H_ */