masaori335 commented on code in PR #13328: URL: https://github.com/apache/trafficserver/pull/13328#discussion_r3548071965
########## src/traffic_ctl/CacheShmCommand.cc: ########## @@ -0,0 +1,261 @@ +/** @file + + traffic_ctl command for inspecting and clearing the cache shared-memory + control segment and its associated stripe segments. + + The status and clear operations work by direct shm_open access rather than + JSONRPC, so they function whether traffic_server is running or not. This + is important for debugging crash-leftover segments when no live process + is available to query. + + @section license License + + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you 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. + */ + +#include "CacheShmCommand.h" +#include "CacheShmLayout.h" +#include "CacheShmPurge.h" +#include "TrafficCtlStatus.h" + +#include <fcntl.h> +#include <sys/mman.h> +#include <sys/stat.h> +#include <unistd.h> + +#include <cerrno> +#include <cstdio> +#include <cstring> +#include <iostream> + +namespace +{ + +// The middle word of the shm name prefix when --prefix is not given. The +// framing "/<word>-" is supplied by cache_shm::normalize_name_prefix, matching +// the server's proxy.config.cache.shm.name_prefix default. +constexpr const char *DEFAULT_PREFIX = "ats"; + +bool +shm_segment_exists(const std::string &name) +{ + int fd = shm_open(name.c_str(), O_RDONLY, 0); + if (fd < 0) { + return false; + } + close(fd); + return true; +} + +std::string +format_size(uint64_t bytes) +{ + char buf[64]; + if (bytes >= (uint64_t{1} << 30)) { + std::snprintf(buf, sizeof(buf), "%.2f GiB", static_cast<double>(bytes) / (uint64_t{1} << 30)); + } else if (bytes >= (uint64_t{1} << 20)) { + std::snprintf(buf, sizeof(buf), "%.2f MiB", static_cast<double>(bytes) / (uint64_t{1} << 20)); + } else if (bytes >= (uint64_t{1} << 10)) { + std::snprintf(buf, sizeof(buf), "%.2f KiB", static_cast<double>(bytes) / (uint64_t{1} << 10)); + } else { + std::snprintf(buf, sizeof(buf), "%llu B", static_cast<unsigned long long>(bytes)); + } + return buf; +} + +// Shared with the cache subsystem (CacheShmPurge.h): read_shm_name bounds a +// possibly-unterminated name field, process_is_alive backs the owner-liveness gate. +using cache_shm::process_is_alive; +using cache_shm::read_shm_name; + +} // namespace + +CacheShmCommand::CacheShmCommand(ts::Arguments *args) : CtrlCommand(args) +{ + if (get_parsed_arguments()->get(STATUS_STR)) { + _invoked_func = [this]() { status(); }; + } else if (get_parsed_arguments()->get(CLEAR_STR)) { + _invoked_func = [this]() { clear(); }; + } +} + +std::string +CacheShmCommand::get_prefix() +{ + // The operator gives only the middle word (e.g. --prefix ats); frame it the + // same way the server does so the two agree on segment names. + std::string configured = DEFAULT_PREFIX; Review Comment: In the case of the prefix in `record.yaml` is changed, but want to remove the old shm from `traffic_ctl` cmd. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
