/* * Copyright (C) 2014 The Android Open Source Project * * 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. */ #include #include #include #include #include #include #include "LogStatistics.h" PidStatistics::PidStatistics(pid_t pid, char *name) : pid(pid) , mSizesTotal(0) , mElementsTotal(0) , mSizes(0) , mElements(0) , name(name) , mGone(false) { } #ifdef DO_NOT_ERROR_IF_PIDSTATISTICS_USES_A_COPY_CONSTRUCTOR PidStatistics::PidStatistics(const PidStatistics ©) : pid(copy->pid) , name(copy->name ? strdup(copy->name) : NULL) , mSizesTotal(copy->mSizesTotal) , mElementsTotal(copy->mElementsTotal) , mSizes(copy->mSizes) , mElements(copy->mElements) , mGone(copy->mGone) { } #endif PidStatistics::~PidStatistics() { free(name); } bool PidStatistics::pidGone() { if (mGone || (pid == gone)) { return true; } if (pid == 0) { return false; } if (kill(pid, 0) && (errno != EPERM)) { mGone = true; return true; } return false; } void PidStatistics::setName(char *new_name) { free(name); name = new_name; } void PidStatistics::add(unsigned short size) { mSizesTotal += size; ++mElementsTotal; mSizes += size; ++mElements; } bool PidStatistics::subtract(unsigned short size) { mSizes -= size; --mElements; return (mElements == 0) && pidGone(); } void PidStatistics::addTotal(size_t size, size_t element) { if (pid == gone) { mSizesTotal += size; mElementsTotal += element; } } // must call free to release return value // If only we could sniff our own logs for: //