| 177 | } |
| 178 | |
| 179 | void |
| 180 | BM_KeyRing_WriteReadRemove(benchmark::State& state) |
| 181 | { |
| 182 | const std::string key_prefix = "benchmark_secret_"; |
| 183 | const std::string secret = "asjdfoilkuj3o8rijsdafo23yo8ifsdjadf"; |
| 184 | if (state.thread_index() == 0) { |
| 185 | } |
| 186 | for (auto _ : state) { |
| 187 | const size_t keys_to_read = state.range(1) / state.threads(); |
| 188 | thread_local size_t bytes_read = 0; |
| 189 | for (size_t i = 0; i < keys_to_read; ++i) { |
| 190 | std::string key(key_prefix); |
| 191 | key.append(std::to_string(state.thread_index())); |
| 192 | const key_serial_t serial = ::add_key( |
| 193 | "user", key.c_str(), secret.c_str(), secret.size(), |
| 194 | KEY_SPEC_PROCESS_KEYRING); |
| 195 | if (serial == -1) { |
| 196 | state.SkipWithError( |
| 197 | std::format( |
| 198 | "add_key: serial:{} i:{} errno:{} err:{}", serial, i, errno, |
| 199 | std::strerror(errno))); |
| 200 | return; |
| 201 | } |
| 202 | std::string out("$", secret.size()); |
| 203 | const auto read = ::keyctl_read(serial, out.data(), out.size()); |
| 204 | if (read < 0) { |
| 205 | state.SkipWithError( |
| 206 | std::format( |
| 207 | "keyctl_read(3): {} errno:{} err:{}", read, errno, |
| 208 | std::strerror(errno))); |
| 209 | return; |
| 210 | } |
| 211 | bytes_read += read; |
| 212 | assert(out == secret); |
| 213 | |
| 214 | const auto inval = ::keyctl_invalidate(serial); |
| 215 | if (inval != 0) { |
| 216 | state.SkipWithError(std::format("keyctl_invalidate(3): {}", inval)); |
| 217 | return; |
| 218 | } |
| 219 | } |
| 220 | state.counters["BytesProcessed"] = benchmark::Counter( |
| 221 | static_cast<double>(bytes_read), benchmark::Counter::kIsRate); |
| 222 | state.counters["KeysProcessed"] = benchmark::Counter( |
| 223 | static_cast<double>(keys_to_read), benchmark::Counter::kIsRate); |
| 224 | state.counters["KeysProcessedInv"] = benchmark::Counter( |
| 225 | static_cast<double>(keys_to_read), |
| 226 | benchmark::Counter::kIsRate | benchmark::Counter::kInvert); |
| 227 | } |
| 228 | if (state.thread_index() == 0) { |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | // note: max number of keys is subject to quota. see /proc/key-users |
| 233 | // increas using /proc/sys/kernel/keys/maxkeys |