The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | #ifndef HAVE_WINSOCK |
| 19 | #define SOCKETLOG |
| 20 | #endif |
| 21 | |
| 22 | #ifdef SOCKETLOG |
| 23 | |
| 24 | #define LOG_TAG "SOCKETLOG" |
| 25 | |
| 26 | #include <string.h> |
| 27 | #include <cutils/log.h> |
| 28 | #include "utils/LogSocket.h" |
| 29 | #include "utils/logger.h" |
| 30 | #include "cutils/hashmap.h" |
| 31 | |
| 32 | // defined in //device/data/etc/event-log-tags |
| 33 | #define SOCKET_CLOSE_LOG 51000 |
| 34 | |
| 35 | static Hashmap* statsMap = NULL; |
| 36 | |
| 37 | #define LOG_LIST_NUMBER 5 |
| 38 | |
| 39 | typedef struct SocketStats { |
| 40 | int fd; |
| 41 | unsigned int send; |
| 42 | unsigned int recv; |
| 43 | unsigned int ip; |
| 44 | unsigned short port; |
| 45 | short reason; |
| 46 | }SocketStats; |
| 47 | |
| 48 | SocketStats *get_socket_stats(int fd) { |
| 49 | if (statsMap == NULL) { |
| 50 | statsMap = hashmapCreate(8, &hashmapIntHash, &hashmapIntEquals); |
| 51 | } |
| 52 | |
| 53 | SocketStats *s = (SocketStats*) hashmapGet(statsMap, &fd); |
| 54 | if (s == NULL) { |
| 55 | // LOGD("create SocketStats for fd %d", fd); |
| 56 | s = (SocketStats*) malloc(sizeof(SocketStats)); |
| 57 | memset(s, 0, sizeof(SocketStats)); |
| 58 | s->fd = fd; |
| 59 | hashmapPut(statsMap, &s->fd, s); |
| 60 | } |
| 61 | return s; |
| 62 | } |
| 63 | |
| 64 | void log_socket_connect(int fd, unsigned int ip, unsigned short port) { |
| 65 | // LOGD("log_socket_connect for fd %d ip %d port%d", fd, ip, port); |
| 66 | SocketStats *s = get_socket_stats(fd); |
| 67 | s->ip = ip; |
| 68 | s->port = port; |
| 69 | } |
| 70 | |
| 71 | void add_send_stats(int fd, int send) { |
| 72 | if (send <=0) { |
| 73 | LOGE("add_send_stats send %d", send); |
| 74 | return; |
| 75 | } |
| 76 | SocketStats *s = get_socket_stats(fd); |
| 77 | s->send += send; |
| 78 | // LOGD("add_send_stats for fd %d ip %d port%d", fd, s->ip, s->port); |
| 79 | } |
| 80 | |
| 81 | void add_recv_stats(int fd, int recv) { |
| 82 | if (recv <=0) { |
| 83 | LOGE("add_recv_stats recv %d", recv); |
| 84 | return; |
| 85 | } |
| 86 | SocketStats *s = get_socket_stats(fd); |
| 87 | s->recv += recv; |
| 88 | // LOGD("add_recv_stats for fd %d ip %d port%d", fd, s->ip, s->port); |
| 89 | } |
| 90 | |
| 91 | char* put_int(char* buf, int value) { |
| 92 | *buf = EVENT_TYPE_INT; |
| 93 | buf++; |
| 94 | memcpy(buf, &value, sizeof(int)); |
| 95 | return buf + sizeof(int); |
| 96 | } |
| 97 | |
| 98 | void log_socket_close(int fd, short reason) { |
| 99 | if (statsMap) { |
| 100 | SocketStats *s = (SocketStats*) hashmapGet(statsMap, &fd); |
| 101 | if (s != NULL) { |
| 102 | if (s->send != 0 || s->recv != 0) { |
| 103 | s->reason = reason; |
| 104 | // 5 int + list type need 2 bytes |
| 105 | char buf[LOG_LIST_NUMBER * 5 + 2]; |
| 106 | buf[0] = EVENT_TYPE_LIST; |
| 107 | buf[1] = LOG_LIST_NUMBER; |
| 108 | char* writePos = buf + 2; |
| 109 | writePos = put_int(writePos, s->send); |
| 110 | writePos = put_int(writePos, s->recv); |
| 111 | writePos = put_int(writePos, s->ip); |
| 112 | writePos = put_int(writePos, s->port); |
| 113 | writePos = put_int(writePos, s->reason); |
| 114 | |
| 115 | android_bWriteLog(SOCKET_CLOSE_LOG, buf, sizeof(buf)); |
| 116 | // LOGD("send %d recv %d reason %d", s->send, s->recv, s->reason); |
| 117 | } |
| 118 | hashmapRemove(statsMap, &s->fd); |
| 119 | free(s); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | #else |
| 125 | void add_send_stats(int fd, int send) {} |
| 126 | void add_recv_stats(int fd, int recv) {} |
| 127 | void log_socket_close(int fd, short reason) {} |
| 128 | void log_socket_connect(int fd, unsigned int ip, unsigned short port) {} |
| 129 | #endif |