Skip to contentSkip to conversation

alfred: Use bitmap to store dataset change events

PATCH Applied Sven Eckelmann 2 participants 4 messages Jul 24, 2017 – Jul 31, 2017

lore
Share & tools
lore
#0 lore raw

cover alfred: Use bitmap to store dataset change events

Hi,

the --update-command implementation looked quite memory inefficient to me and I have therefore replaced it using a simple bit map which stores in BIT(i) when datatype I was modified. Here from the second patch:

Quoted 15 lines

The implementation for --update-command uses a double linked list to store the detected modifications of datasets. This implementation requires 24 bytes (1 byte data type, 7 bytes padding, 16 bytes linked list pointers) on amd64 for each element of the list. Each modified dataset type requires an own list item. Each list item is allocated using malloc and therefore additional overhead for the libc allocator information has also to be stored next to it. The list head uses a similar amount of memory (16 byte list pointers, 2 byte counter, 6 byte padding) but doesn't require additional allocations.

The list based implementation provides information about modified dataset type and the order in which these modifications were detected. But the latter is not actually required fr --update-command and can therefore be omitted.

Quoted 5 lines

A simple 256 bit wide storage element (32 bytes) is enough to keep the required information when only the detected modified data types have to be registered. BIT(i) is either 1 when dataset type i was modified or 0 when no modification was detected. This removes the (de)allocation CPU + memory overhead and avoids list searches when adding newly detected modifications.

The bitops.h file is used to implement the bit operations and keep the details away from the server code. I've reduced the size of the header file a little bit because most of it was not used by alfred.

Kind regards,
Sven

Sven Eckelmann (2):
alfred: Use constant to define data type range
alfred: Use bitmap to store dataset change events

 alfred.h |   4 +-
 bitops.h | 379 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 main.c   |   9 +-
 packet.h |   1 +
 server.c |  46 ++++----
 5 files changed, 406 insertions(+), 33 deletions(-)
 create mode 100644 bitops.h
#1 lore raw

PATCH 1/2 alfred: Use constant to define data type range

The maximum number of data types supported by alfred is limited by the range of an uint8_t (0-255). alfred checks in the client mode whether data type parameter is in the supported range by comparing it against ALFRED_MAX_RESERVED_TYPE as lower bound and the magic number 255 as upper bound.

Instead of using 255 in multiple places, define a named constant which can be referenced. This makes it easier to understand these comparisons and to reference this limit in a consistent way.

---
 main.c   | 6 ++++--
 packet.h | 1 +
 2 files changed, 5 insertions(+), 2 deletions(-)
main.c +4 -2
index 446eecf..ad6d081 100644
@@ -213,7 +213,8 @@ static struct globals *alfred_init(int argc, char *argv[])
 		case 'r':
 			globals->clientmode = CLIENT_REQUEST_DATA;
 			i = atoi(optarg);
-			if (i < ALFRED_MAX_RESERVED_TYPE || i > 255) {
+			if (i < ALFRED_MAX_RESERVED_TYPE ||
+			    i >= ALFRED_NUM_TYPES) {
 				fprintf(stderr, "bad data type argument\n");
 				return NULL;
 			}
@@ -223,7 +224,8 @@ static struct globals *alfred_init(int argc, char *argv[])
 		case 's':
 			globals->clientmode = CLIENT_SET_DATA;
 			i = atoi(optarg);
-			if (i < ALFRED_MAX_RESERVED_TYPE || i > 255) {
+			if (i < ALFRED_MAX_RESERVED_TYPE ||
+			    i >= ALFRED_NUM_TYPES) {
 				fprintf(stderr, "bad data type argument\n");
 				return NULL;
 			}
packet.h +1 -0
index afd3715..425459f 100644
@@ -177,5 +177,6 @@ struct alfred_status_v0 {
 #define ALFRED_VERSION			0
 #define ALFRED_PORT			0x4242
 #define ALFRED_MAX_RESERVED_TYPE	64
+#define ALFRED_NUM_TYPES		256
 
 #endif
Signature · 1 line
2.11.0
#2 lore raw

PATCH 2/2 alfred: Use bitmap to store dataset change events

In reply to Sven Eckelmann

The implementation for --update-command uses a double linked list to store the detected modifications of datasets. This implementation requires 24 bytes (1 byte data type, 7 bytes padding, 16 bytes linked list pointers) on amd64 for each element of the list. Each modified dataset type requires an own list item. Each list item is allocated using malloc and therefore additional overhead for the libc allocator information has also to be stored next to it. The list head uses a similar amount of memory (16 byte list pointers, 2 byte counter, 6 byte padding) but doesn't require additional allocations.

The list based implementation provides information about modified dataset type and the order in which these modifications were detected. But the latter is not actually required fr --update-command and can therefore be omitted.

A simple 256 bit wide storage element (32 bytes) is enough to keep the required information when only the detected modified data types have to be registered. BIT(i) is either 1 when dataset type i was modified or 0 when no modification was detected. This removes the (de)allocation CPU + memory overhead and avoids list searches when adding newly detected modifications.

Cc Anatoliy Lapitskiy <[email protected]>
---
 alfred.h |   4 +-
 bitops.h | 379 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 main.c   |   3 +-
 server.c |  46 ++++----
 4 files changed, 401 insertions(+), 31 deletions(-)
 create mode 100644 bitops.h
alfred.h +2 -2
index 53d8b7a..d559d95 100644
@@ -31,6 +31,7 @@
 #include <time.h>
 #include <sys/select.h>
 #include <sys/types.h>
+#include "bitops.h"
 #include "list.h"
 #include "packet.h"
 
@@ -134,8 +135,7 @@ struct globals {
 	const char *unix_path;
 
 	const char *update_command;
-	struct list_head changed_data_types;
-	uint16_t changed_data_type_count; /* maximum is 256 */
+	DECLARE_BITMAP(changed_data_types, ALFRED_NUM_TYPES);
 
 	struct timespec if_check;
 	struct timespec sync_period;
bitops.h new file +379 -0

379 lines not on this page · raw message

main.c +1 -2
index ad6d081..cf1e460 100644
@@ -202,8 +202,7 @@ static struct globals *alfred_init(int argc, char *argv[])
 	globals->update_command = NULL;
 	globals->sync_period.tv_sec = ALFRED_INTERVAL;
 	globals->sync_period.tv_nsec = 0;
-	INIT_LIST_HEAD(&globals->changed_data_types);
-	globals->changed_data_type_count = 0;
+	bitmap_zero(globals->changed_data_types, ALFRED_NUM_TYPES);
 
 	time_random_seed();
 
server.c +19 -27

85 lines not on this page · raw message

Signature · 1 line
2.11.0
#3 lore raw
In reply to Sven Eckelmann
Quoted 29 lines · CEST Sven Eckelmann

Hi,

the --update-command implementation looked quite memory inefficient to me and I have therefore replaced it using a simple bit map which stores in BIT(i)
when datatype I was modified. Here from the second patch:

Quoted 16 lines

The implementation for --update-command uses a double linked list to store the detected modifications of datasets. This implementation requires 24 bytes (1 byte data type, 7 bytes padding, 16 bytes linked list pointers) on
amd64 for each element of the list. Each modified dataset type requires an own list item. Each list item is allocated using malloc and therefore additional overhead for the libc allocator information has also to be stored next to it. The list head uses a similar amount of memory (16 byte list pointers, 2 byte counter, 6 byte padding) but doesn't require additional allocations.

The list based implementation provides information about modified dataset type and the order in which these modifications were detected. But the latter is not actually required fr --update-command and can therefore be omitted.

Quoted 6 lines

A simple 256 bit wide storage element (32 bytes) is enough to keep the required information when only the detected modified data types have to be registered. BIT(i) is either 1 when dataset type i was modified or 0 when no modification was detected. This removes the (de)allocation CPU + memory overhead and avoids list searches when adding newly detected modifications.

Quoted 3 lines

The bitops.h file is used to implement the bit operations and keep the details away from the server code. I've reduced the size of the header file a little bit because most of it was not used by alfred.

Thanks, applied this series

Cheers,
Simon