be/src/runtime/cache/cache_utils.h
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #pragma once |
19 | | |
20 | | #include <sys/time.h> |
21 | | |
22 | | #include <algorithm> |
23 | | #include <cassert> |
24 | | #include <cstdio> |
25 | | #include <cstdlib> |
26 | | #include <exception> |
27 | | #include <iostream> |
28 | | #include <list> |
29 | | #include <map> |
30 | | #include <shared_mutex> |
31 | | #include <thread> |
32 | | |
33 | | namespace doris { |
34 | | |
35 | | typedef std::shared_lock<std::shared_mutex> CacheReadLock; |
36 | | typedef std::unique_lock<std::shared_mutex> CacheWriteLock; |
37 | | |
38 | | struct CacheStat { |
39 | | static const uint32_t DAY_SECONDS = 86400; |
40 | | long cache_time; |
41 | | long last_update_time; |
42 | | long last_read_time; |
43 | | uint32_t read_count; |
44 | 10.2k | CacheStat() { init(); } |
45 | | |
46 | 10.2k | long cache_time_second() { |
47 | 10.2k | struct timeval tv; |
48 | 10.2k | gettimeofday(&tv, nullptr); |
49 | 10.2k | return tv.tv_sec; |
50 | 10.2k | } |
51 | | |
52 | 20.5k | void init() { |
53 | 20.5k | cache_time = 0; |
54 | 20.5k | last_update_time = 0; |
55 | 20.5k | last_read_time = 0; |
56 | 20.5k | read_count = 0; |
57 | 20.5k | } |
58 | | |
59 | 10.2k | void update() { |
60 | 10.2k | last_update_time = cache_time_second(); |
61 | 10.2k | if (cache_time == 0) { |
62 | 10.2k | cache_time = last_update_time; |
63 | 10.2k | } |
64 | 10.2k | last_read_time = last_update_time; |
65 | 10.2k | read_count++; |
66 | 10.2k | } |
67 | | |
68 | 7 | void query() { |
69 | 7 | last_read_time = cache_time_second(); |
70 | 7 | read_count++; |
71 | 7 | } |
72 | | |
73 | 0 | double last_query_day() { return (cache_time_second() - last_read_time) * 1.0 / DAY_SECONDS; } |
74 | | |
75 | 0 | double avg_query_count() { |
76 | 0 | return read_count * DAY_SECONDS * 1.0 / (cache_time_second() - last_read_time + 1); |
77 | 0 | } |
78 | | }; |
79 | | |
80 | | } // namespace doris |