Lines Matching refs:event
87 AStatsEvent* event = malloc(sizeof(AStatsEvent)); in AStatsEvent_obtain() local
88 event->lastFieldPos = 0; in AStatsEvent_obtain()
89 event->numBytesWritten = 2; // reserve first 2 bytes for root event type and number of elements in AStatsEvent_obtain()
90 event->numElements = 0; in AStatsEvent_obtain()
91 event->atomId = 0; in AStatsEvent_obtain()
92 event->errors = 0; in AStatsEvent_obtain()
93 event->built = false; in AStatsEvent_obtain()
94 event->bufSize = MAX_PUSH_EVENT_PAYLOAD; in AStatsEvent_obtain()
95 event->buf = (uint8_t*)calloc(event->bufSize, 1); in AStatsEvent_obtain()
97 event->buf[0] = OBJECT_TYPE; in AStatsEvent_obtain()
98 AStatsEvent_writeInt64(event, get_elapsed_realtime_ns()); // write the timestamp in AStatsEvent_obtain()
100 return event; in AStatsEvent_obtain()
103 void AStatsEvent_release(AStatsEvent* event) { in AStatsEvent_release() argument
104 free(event->buf); in AStatsEvent_release()
105 free(event); in AStatsEvent_release()
108 void AStatsEvent_setAtomId(AStatsEvent* event, uint32_t atomId) { in AStatsEvent_setAtomId() argument
109 if (event->atomId != 0) return; in AStatsEvent_setAtomId()
110 if (event->numElements != 1) { in AStatsEvent_setAtomId()
111 event->errors |= ERROR_ATOM_ID_INVALID_POSITION; in AStatsEvent_setAtomId()
115 event->atomId = atomId; in AStatsEvent_setAtomId()
116 AStatsEvent_writeInt32(event, atomId); in AStatsEvent_setAtomId()
121 void AStatsEvent_overwriteTimestamp(AStatsEvent* event, uint64_t timestampNs) { in AStatsEvent_overwriteTimestamp() argument
122 memcpy(&event->buf[POS_TIMESTAMP + sizeof(uint8_t)], ×tampNs, sizeof(timestampNs)); in AStatsEvent_overwriteTimestamp()
128 static bool overflows(AStatsEvent* event, size_t size) { in overflows() argument
129 const size_t totalBytesNeeded = event->numBytesWritten + size; in overflows()
131 event->errors |= ERROR_OVERFLOW; in overflows()
136 if (event->bufSize < MAX_PULL_EVENT_PAYLOAD && totalBytesNeeded > event->bufSize) { in overflows()
138 event->bufSize *= 2; in overflows()
139 } while (event->bufSize <= totalBytesNeeded); in overflows()
141 if (event->bufSize > MAX_PULL_EVENT_PAYLOAD) { in overflows()
142 event->bufSize = MAX_PULL_EVENT_PAYLOAD; in overflows()
145 event->buf = (uint8_t*)realloc(event->buf, event->bufSize); in overflows()
152 static void append_byte(AStatsEvent* event, uint8_t value) { in append_byte() argument
153 if (!overflows(event, sizeof(value))) { in append_byte()
154 event->buf[event->numBytesWritten] = value; in append_byte()
155 event->numBytesWritten += sizeof(value); in append_byte()
159 static void append_bool(AStatsEvent* event, bool value) { in append_bool() argument
160 append_byte(event, (uint8_t)value); in append_bool()
163 static void append_int32(AStatsEvent* event, int32_t value) { in append_int32() argument
164 if (!overflows(event, sizeof(value))) { in append_int32()
165 memcpy(&event->buf[event->numBytesWritten], &value, sizeof(value)); in append_int32()
166 event->numBytesWritten += sizeof(value); in append_int32()
170 static void append_int64(AStatsEvent* event, int64_t value) { in append_int64() argument
171 if (!overflows(event, sizeof(value))) { in append_int64()
172 memcpy(&event->buf[event->numBytesWritten], &value, sizeof(value)); in append_int64()
173 event->numBytesWritten += sizeof(value); in append_int64()
177 static void append_float(AStatsEvent* event, float value) { in append_float() argument
178 if (!overflows(event, sizeof(value))) { in append_float()
179 memcpy(&event->buf[event->numBytesWritten], &value, sizeof(value)); in append_float()
180 event->numBytesWritten += sizeof(float); in append_float()
184 static void append_byte_array(AStatsEvent* event, const uint8_t* buf, size_t size) { in append_byte_array() argument
185 if (!overflows(event, size)) { in append_byte_array()
186 memcpy(&event->buf[event->numBytesWritten], buf, size); in append_byte_array()
187 event->numBytesWritten += size; in append_byte_array()
192 static void append_string(AStatsEvent* event, const char* buf) { in append_string() argument
195 event->errors |= ERROR_STRING_NOT_NULL_TERMINATED; in append_string()
199 append_int32(event, size); in append_string()
200 append_byte_array(event, (uint8_t*)buf, size); in append_string()
203 static void start_field(AStatsEvent* event, uint8_t typeId) { in start_field() argument
204 event->lastFieldPos = event->numBytesWritten; in start_field()
205 append_byte(event, typeId); in start_field()
206 event->numElements++; in start_field()
209 void AStatsEvent_writeInt32(AStatsEvent* event, int32_t value) { in AStatsEvent_writeInt32() argument
210 start_field(event, INT32_TYPE); in AStatsEvent_writeInt32()
211 append_int32(event, value); in AStatsEvent_writeInt32()
214 void AStatsEvent_writeInt64(AStatsEvent* event, int64_t value) { in AStatsEvent_writeInt64() argument
215 start_field(event, INT64_TYPE); in AStatsEvent_writeInt64()
216 append_int64(event, value); in AStatsEvent_writeInt64()
219 void AStatsEvent_writeFloat(AStatsEvent* event, float value) { in AStatsEvent_writeFloat() argument
220 start_field(event, FLOAT_TYPE); in AStatsEvent_writeFloat()
221 append_float(event, value); in AStatsEvent_writeFloat()
224 void AStatsEvent_writeBool(AStatsEvent* event, bool value) { in AStatsEvent_writeBool() argument
225 start_field(event, BOOL_TYPE); in AStatsEvent_writeBool()
226 append_bool(event, value); in AStatsEvent_writeBool()
229 void AStatsEvent_writeByteArray(AStatsEvent* event, const uint8_t* buf, size_t numBytes) { in AStatsEvent_writeByteArray() argument
230 start_field(event, BYTE_ARRAY_TYPE); in AStatsEvent_writeByteArray()
234 append_int32(event, numBytes); in AStatsEvent_writeByteArray()
236 append_byte_array(event, buf, numBytes); in AStatsEvent_writeByteArray()
241 void AStatsEvent_writeString(AStatsEvent* event, const char* value) { in AStatsEvent_writeString() argument
242 start_field(event, STRING_TYPE); in AStatsEvent_writeString()
243 append_string(event, value == NULL ? "" : value); in AStatsEvent_writeString()
247 void AStatsEvent_writeAttributionChain(AStatsEvent* event, const uint32_t* uids, in AStatsEvent_writeAttributionChain() argument
250 event->errors |= ERROR_ATTRIBUTION_CHAIN_TOO_LONG; in AStatsEvent_writeAttributionChain()
254 start_field(event, ATTRIBUTION_CHAIN_TYPE); in AStatsEvent_writeAttributionChain()
255 append_byte(event, numNodes); in AStatsEvent_writeAttributionChain()
258 append_int32(event, uids[i]); in AStatsEvent_writeAttributionChain()
259 append_string(event, tags[i] == NULL ? "" : tags[i]); in AStatsEvent_writeAttributionChain()
263 static bool writeArrayMetadata(AStatsEvent* event, size_t numElements, uint8_t elementTypeId) { in writeArrayMetadata() argument
265 event->errors |= ERROR_LIST_TOO_LONG; in writeArrayMetadata()
269 start_field(event, LIST_TYPE); in writeArrayMetadata()
270 append_byte(event, numElements); in writeArrayMetadata()
271 append_byte(event, elementTypeId); in writeArrayMetadata()
275 void AStatsEvent_writeInt32Array(AStatsEvent* event, const int32_t* elements, size_t numElements) { in AStatsEvent_writeInt32Array() argument
276 if (!writeArrayMetadata(event, numElements, INT32_TYPE)) { in AStatsEvent_writeInt32Array()
281 append_int32(event, elements[i]); in AStatsEvent_writeInt32Array()
285 void AStatsEvent_writeInt64Array(AStatsEvent* event, const int64_t* elements, size_t numElements) { in AStatsEvent_writeInt64Array() argument
286 if (!writeArrayMetadata(event, numElements, INT64_TYPE)) { in AStatsEvent_writeInt64Array()
291 append_int64(event, elements[i]); in AStatsEvent_writeInt64Array()
295 void AStatsEvent_writeFloatArray(AStatsEvent* event, const float* elements, size_t numElements) { in AStatsEvent_writeFloatArray() argument
296 if (!writeArrayMetadata(event, numElements, FLOAT_TYPE)) { in AStatsEvent_writeFloatArray()
301 append_float(event, elements[i]); in AStatsEvent_writeFloatArray()
305 void AStatsEvent_writeBoolArray(AStatsEvent* event, const bool* elements, size_t numElements) { in AStatsEvent_writeBoolArray() argument
306 if (!writeArrayMetadata(event, numElements, BOOL_TYPE)) { in AStatsEvent_writeBoolArray()
311 append_bool(event, elements[i]); in AStatsEvent_writeBoolArray()
315 void AStatsEvent_writeStringArray(AStatsEvent* event, const char* const* elements, in AStatsEvent_writeStringArray() argument
317 if (!writeArrayMetadata(event, numElements, STRING_TYPE)) { in AStatsEvent_writeStringArray()
322 append_string(event, elements[i] == NULL ? "" : elements[i]); in AStatsEvent_writeStringArray()
327 static void increment_annotation_count(AStatsEvent* event) { in increment_annotation_count() argument
328 if (event->lastFieldPos >= event->bufSize) { in increment_annotation_count()
331 uint8_t fieldType = event->buf[event->lastFieldPos] & 0x0F; in increment_annotation_count()
332 uint32_t oldAnnotationCount = (event->buf[event->lastFieldPos] & 0xF0) >> 4; in increment_annotation_count()
336 event->errors |= ERROR_TOO_MANY_ANNOTATIONS; in increment_annotation_count()
340 event->buf[event->lastFieldPos] = (((uint8_t)newAnnotationCount << 4) & 0xF0) | fieldType; in increment_annotation_count()
343 void AStatsEvent_addBoolAnnotation(AStatsEvent* event, uint8_t annotationId, bool value) { in AStatsEvent_addBoolAnnotation() argument
344 if (event->numElements < 2) { in AStatsEvent_addBoolAnnotation()
345 event->errors |= ERROR_ANNOTATION_DOES_NOT_FOLLOW_FIELD; in AStatsEvent_addBoolAnnotation()
348 event->errors |= ERROR_ANNOTATION_ID_TOO_LARGE; in AStatsEvent_addBoolAnnotation()
352 append_byte(event, annotationId); in AStatsEvent_addBoolAnnotation()
353 append_byte(event, BOOL_TYPE); in AStatsEvent_addBoolAnnotation()
354 append_bool(event, value); in AStatsEvent_addBoolAnnotation()
355 increment_annotation_count(event); in AStatsEvent_addBoolAnnotation()
358 void AStatsEvent_addInt32Annotation(AStatsEvent* event, uint8_t annotationId, int32_t value) { in AStatsEvent_addInt32Annotation() argument
359 if (event->numElements < 2) { in AStatsEvent_addInt32Annotation()
360 event->errors |= ERROR_ANNOTATION_DOES_NOT_FOLLOW_FIELD; in AStatsEvent_addInt32Annotation()
363 event->errors |= ERROR_ANNOTATION_ID_TOO_LARGE; in AStatsEvent_addInt32Annotation()
367 append_byte(event, annotationId); in AStatsEvent_addInt32Annotation()
368 append_byte(event, INT32_TYPE); in AStatsEvent_addInt32Annotation()
369 append_int32(event, value); in AStatsEvent_addInt32Annotation()
370 increment_annotation_count(event); in AStatsEvent_addInt32Annotation()
373 uint32_t AStatsEvent_getAtomId(AStatsEvent* event) { in AStatsEvent_getAtomId() argument
374 return event->atomId; in AStatsEvent_getAtomId()
377 uint8_t* AStatsEvent_getBuffer(AStatsEvent* event, size_t* size) { in AStatsEvent_getBuffer() argument
378 if (size) *size = event->numBytesWritten; in AStatsEvent_getBuffer()
379 return event->buf; in AStatsEvent_getBuffer()
382 uint32_t AStatsEvent_getErrors(AStatsEvent* event) { in AStatsEvent_getErrors() argument
383 return event->errors; in AStatsEvent_getErrors()
386 static void build_internal(AStatsEvent* event, const bool push) { in build_internal() argument
387 if (event->numElements > MAX_BYTE_VALUE) event->errors |= ERROR_TOO_MANY_FIELDS; in build_internal()
388 if (0 == event->atomId) event->errors |= ERROR_NO_ATOM_ID; in build_internal()
389 if (push && event->numBytesWritten > MAX_PUSH_EVENT_PAYLOAD) event->errors |= ERROR_OVERFLOW; in build_internal()
392 if (event->errors) { in build_internal()
395 event->numElements = 2; in build_internal()
397 event->buf[POS_ATOM_ID] = INT32_TYPE; in build_internal()
399 event->numBytesWritten = POS_ATOM_ID + sizeof(uint8_t) + sizeof(uint32_t); in build_internal()
400 start_field(event, ERROR_TYPE); in build_internal()
401 append_int32(event, event->errors); in build_internal()
404 event->buf[POS_NUM_ELEMENTS] = event->numElements; in build_internal()
407 void AStatsEvent_build(AStatsEvent* event) { in AStatsEvent_build() argument
408 if (event->built) return; in AStatsEvent_build()
410 build_internal(event, false /* push */); in AStatsEvent_build()
412 event->built = true; in AStatsEvent_build()
415 int AStatsEvent_write(AStatsEvent* event) { in AStatsEvent_write() argument
416 build_internal(event, true /* push */); in AStatsEvent_write()
417 return write_buffer_to_statsd(event->buf, event->numBytesWritten, event->atomId); in AStatsEvent_write()