Merge "logd: Add pidToName helper"
This commit is contained in:
commit
ebdf9778a0
3 changed files with 69 additions and 6 deletions
|
|
@ -77,6 +77,9 @@ public:
|
||||||
void formatPrune(char **strp) { mPrune.format(strp); }
|
void formatPrune(char **strp) { mPrune.format(strp); }
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// helper
|
||||||
|
char *pidToName(pid_t pid) { return stats.pidToName(pid); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void maybePrune(log_id_t id);
|
void maybePrune(log_id_t id);
|
||||||
void prune(log_id_t id, unsigned long pruneRows);
|
void prune(log_id_t id, unsigned long pruneRows);
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <fcntl.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
|
|
@ -23,12 +24,34 @@
|
||||||
|
|
||||||
#include "LogStatistics.h"
|
#include "LogStatistics.h"
|
||||||
|
|
||||||
PidStatistics::PidStatistics(pid_t pid)
|
PidStatistics::PidStatistics(pid_t pid, char *name)
|
||||||
: pid(pid)
|
: pid(pid)
|
||||||
, mSizesTotal(0)
|
, mSizesTotal(0)
|
||||||
, mElementsTotal(0)
|
, mElementsTotal(0)
|
||||||
, mSizes(0)
|
, mSizes(0)
|
||||||
, mElements(0) { }
|
, mElements(0)
|
||||||
|
, name(name)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
#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)
|
||||||
|
{ }
|
||||||
|
#endif
|
||||||
|
|
||||||
|
PidStatistics::~PidStatistics() {
|
||||||
|
free(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PidStatistics::setName(char *new_name) {
|
||||||
|
free(name);
|
||||||
|
name = new_name;
|
||||||
|
}
|
||||||
|
|
||||||
void PidStatistics::add(unsigned short size) {
|
void PidStatistics::add(unsigned short size) {
|
||||||
mSizesTotal += size;
|
mSizesTotal += size;
|
||||||
|
|
@ -50,6 +73,28 @@ void PidStatistics::addTotal(size_t size, size_t element) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// must call free to release return value
|
||||||
|
char *PidStatistics::pidToName(pid_t pid) {
|
||||||
|
char *retval = NULL;
|
||||||
|
if (pid != PidStatistics::gone) {
|
||||||
|
char buffer[512];
|
||||||
|
snprintf(buffer, sizeof(buffer), "/proc/%u/cmdline", pid);
|
||||||
|
int fd = open(buffer, O_RDONLY);
|
||||||
|
if (fd >= 0) {
|
||||||
|
ssize_t ret = read(fd, buffer, sizeof(buffer));
|
||||||
|
if (ret > 0) {
|
||||||
|
buffer[sizeof(buffer)-1] = '\0';
|
||||||
|
// frameworks intermediate state
|
||||||
|
if (strcmp(buffer, "<pre-initialized>")) {
|
||||||
|
retval = strdup(buffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
close(fd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
|
||||||
UidStatistics::UidStatistics(uid_t uid)
|
UidStatistics::UidStatistics(uid_t uid)
|
||||||
: uid(uid) {
|
: uid(uid) {
|
||||||
Pids.clear();
|
Pids.clear();
|
||||||
|
|
@ -83,7 +128,7 @@ void UidStatistics::add(unsigned short size, pid_t pid) {
|
||||||
bool insert = (last != it)
|
bool insert = (last != it)
|
||||||
&& ((p->getPid() == p->gone)
|
&& ((p->getPid() == p->gone)
|
||||||
|| ((*last)->sizesTotal() < (size_t) size));
|
|| ((*last)->sizesTotal() < (size_t) size));
|
||||||
p = new PidStatistics(pid);
|
p = new PidStatistics(pid, pidToName(pid));
|
||||||
if (insert) {
|
if (insert) {
|
||||||
Pids.insert(last, p);
|
Pids.insert(last, p);
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -397,8 +442,8 @@ size_t LogStatistics::elementsTotal(log_id_t log_id, uid_t uid, pid_t pid) {
|
||||||
|
|
||||||
void LogStatistics::format(char **buf,
|
void LogStatistics::format(char **buf,
|
||||||
uid_t uid, unsigned int logMask, log_time oldest) {
|
uid_t uid, unsigned int logMask, log_time oldest) {
|
||||||
const unsigned short spaces_current = 13;
|
static const unsigned short spaces_current = 13;
|
||||||
const unsigned short spaces_total = 19;
|
static const unsigned short spaces_total = 19;
|
||||||
|
|
||||||
if (*buf) {
|
if (*buf) {
|
||||||
free(buf);
|
free(buf);
|
||||||
|
|
|
||||||
|
|
@ -36,12 +36,18 @@ class PidStatistics {
|
||||||
size_t mSizes;
|
size_t mSizes;
|
||||||
size_t mElements;
|
size_t mElements;
|
||||||
|
|
||||||
|
char *name;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static const pid_t gone = (pid_t) -1;
|
static const pid_t gone = (pid_t) -1;
|
||||||
|
|
||||||
PidStatistics(pid_t pid);
|
PidStatistics(pid_t pid, char *name = NULL);
|
||||||
|
PidStatistics(const PidStatistics ©);
|
||||||
|
~PidStatistics();
|
||||||
|
|
||||||
pid_t getPid() const { return pid; }
|
pid_t getPid() const { return pid; }
|
||||||
|
char *getName() const { return name; }
|
||||||
|
void setName(char *name);
|
||||||
|
|
||||||
void add(unsigned short size);
|
void add(unsigned short size);
|
||||||
bool subtract(unsigned short size); // returns true if stats and PID gone
|
bool subtract(unsigned short size); // returns true if stats and PID gone
|
||||||
|
|
@ -52,6 +58,9 @@ public:
|
||||||
|
|
||||||
size_t sizesTotal() const { return mSizesTotal; }
|
size_t sizesTotal() const { return mSizesTotal; }
|
||||||
size_t elementsTotal() const { return mElementsTotal; }
|
size_t elementsTotal() const { return mElementsTotal; }
|
||||||
|
|
||||||
|
// helper
|
||||||
|
static char *pidToName(pid_t pid);
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef android::List<PidStatistics *> PidStatisticsCollection;
|
typedef android::List<PidStatistics *> PidStatisticsCollection;
|
||||||
|
|
@ -80,6 +89,9 @@ public:
|
||||||
|
|
||||||
size_t sizesTotal(pid_t pid = pid_all);
|
size_t sizesTotal(pid_t pid = pid_all);
|
||||||
size_t elementsTotal(pid_t pid = pid_all);
|
size_t elementsTotal(pid_t pid = pid_all);
|
||||||
|
|
||||||
|
// helper
|
||||||
|
static char *pidToName(pid_t pid) { return PidStatistics::pidToName(pid); }
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef android::List<UidStatistics *> UidStatisticsCollection;
|
typedef android::List<UidStatistics *> UidStatisticsCollection;
|
||||||
|
|
@ -157,6 +169,9 @@ public:
|
||||||
|
|
||||||
// *strp = malloc, balance with free
|
// *strp = malloc, balance with free
|
||||||
void format(char **strp, uid_t uid, unsigned int logMask, log_time oldest);
|
void format(char **strp, uid_t uid, unsigned int logMask, log_time oldest);
|
||||||
|
|
||||||
|
// helper
|
||||||
|
static char *pidToName(pid_t pid) { return PidStatistics::pidToName(pid); }
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // _LOGD_LOG_STATISTICS_H__
|
#endif // _LOGD_LOG_STATISTICS_H__
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue