File: sync_stats.h

package info (click to toggle)
vulkan-validationlayers 1.4.321.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 47,412 kB
  • sloc: cpp: 594,175; python: 11,321; sh: 24; makefile: 20; xml: 14
file content (127 lines) | stat: -rw-r--r-- 3,519 bytes parent folder | download | duplicates (2)
1
2
3
4
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
35
36
37
38
39
40
41
42
43
44
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* Copyright (c) 2025 The Khronos Group Inc.
 * Copyright (c) 2025 Valve Corporation
 * Copyright (c) 2025 LunarG, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <string>
#include <cstdint>

#ifndef VVL_ENABLE_SYNCVAL_STATS
#define VVL_ENABLE_SYNCVAL_STATS 0
#endif

#if VVL_ENABLE_SYNCVAL_STATS != 0
#include <atomic>

// NOTE: mimalloc should be built with MI_STAT=1 to enable stats module
#if defined(USE_MIMALLOC)
#include <mimalloc.h>
#if MI_MALLOC_VERSION >= 300
#define USE_MIMALLOC_STATS
#include "mimalloc-stats.h"
#include <mutex>
#endif
#endif  // defined(USE_MIMALLOC)

#endif  // VVL_ENABLE_SYNCVAL_STATS != 0

namespace syncval_stats {
#if VVL_ENABLE_SYNCVAL_STATS != 0

struct Value32 {
    std::atomic_uint32_t u32;
    void Update(uint32_t new_value);
    uint32_t Add(uint32_t n);  // Returns new counter value
    uint32_t Sub(uint32_t n);  // Returns new counter value
};

struct Value64 {
    std::atomic_uint64_t u64;
    void Update(uint64_t new_value);
    uint64_t Add(uint64_t n);  // Returns new counter value
    uint64_t Sub(uint64_t n);  // Returns new counter value
};

struct ValueMax32 {
    Value32 value;
    Value32 max_value;
    void Update(uint32_t new_value);
    void Add(uint32_t n);
    void Sub(uint32_t n);
};

struct ValueMax64 {
    Value64 value;
    Value64 max_value;
    void Update(uint64_t new_value);
    void Add(uint64_t n);
    void Sub(uint64_t n);
};

struct Stats {
    ~Stats();
    bool report_on_destruction = false;

#if defined(USE_MIMALLOC_STATS)
    mi_stats_t mi_stats;
    std::mutex mi_stats_mutex;
#endif

    ValueMax32 command_buffer_context_counter;
    void AddCommandBufferContext();
    void RemoveCommandBufferContext();

    ValueMax32 queue_batch_context_counter;
    void AddQueueBatchContext();
    void RemoveQueueBatchContext();

    ValueMax32 timeline_signal_counter;
    void AddTimelineSignals(uint32_t count);
    void RemoveTimelineSignals(uint32_t count);

    ValueMax32 unresolved_batch_counter;
    void AddUnresolvedBatch();
    void RemoveUnresolvedBatch();

    ValueMax32 handle_record_counter;
    void AddHandleRecord(uint32_t count = 1);
    void RemoveHandleRecord(uint32_t count = 1);

    void UpdateMemoryStats();
    void ReportOnDestruction();
    std::string CreateReport();
};

#else
struct Stats {
    void AddHandleRecord(uint32_t count = 1) {}
    void RemoveHandleRecord(uint32_t count = 1) {}
    void AddCommandBufferContext() {}
    void RemoveCommandBufferContext() {}
    void AddQueueBatchContext() {}
    void RemoveQueueBatchContext() {}
    void AddTimelineSignals(uint32_t count) {}
    void RemoveTimelineSignals(uint32_t count) {}
    void AddUnresolvedBatch() {}
    void RemoveUnresolvedBatch() {}

    void UpdateMemoryStats() {}
    void ReportOnDestruction() {}
    std::string CreateReport() { return "SyncVal stats are disabled in the current build configuration\n"; }
};
#endif  // VVL_ENABLE_SYNCVAL_STATS != 0
}  // namespace syncval_stats