sailfish_datahistory.c 13.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
/*
 *  Connection Manager
 *
 *  Copyright (C) 2016-2018 Jolla Ltd. All rights reserved.
 *  Copyright (C) 2016-2018 Slava Monich <slava.monich@jolla.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 */

#include "sailfish_datahistory_p.h"

#include "connman.h"

#include <gutil_misc.h>
#include <gutil_idlepool.h>

enum datahistory_counter_events {
	EVENT_RESET,
	EVENT_UPDATE,
	EVENT_COUNT
};

struct datahistory_priv {
	GUtilIdlePool* pool;
	GDateTime* start_time;
	GDateTime* next_period;
	guint update_interval;
	gulong event_id[EVENT_COUNT];
};

G_DEFINE_ABSTRACT_TYPE(DataHistory, datahistory, G_TYPE_OBJECT)
#define PARENT_CLASS datahistory_parent_class
#define DATAHISTORY_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj),\
        DATAHISTORY_TYPE, DataHistoryClass)
#define DATAHISTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), \
	DATAHISTORY_TYPE, DataHistory))

enum datahistory_signal {
	SIGNAL_CLEARED,
	SIGNAL_START_TIME_CHANGED,
	SIGNAL_LAST_SAMPLE_CHANGED,
	SIGNAL_SAMPLE_ADDED,
	SIGNAL_COUNT
};

#define SIGNAL_CLEARED_NAME             "datahistory-cleared"
#define SIGNAL_START_TIME_CHANGED_NAME  "datahistory-start-time-changed"
#define SIGNAL_LAST_SAMPLE_CHANGED_NAME "datahistory-last-sample-changed"
#define SIGNAL_SAMPLE_ADDED_NAME        "datahistory-sample-added"

static guint datahistory_signal[SIGNAL_COUNT];

static const guint32 datahistory_time_interval [] = {
	1,                              /* TIME_UNIT_SECOND */
	60,                             /* TIME_UNIT_MINUTE */
	60*60,                          /* TIME_UNIT_HOUR */
	60*60*24,                       /* TIME_UNIT_DAY */
	60*60*24*28,                    /* TIME_UNIT_MONTH */
	60*60*24*365                    /* TIME_UNIT_YEAR */
};

G_STATIC_ASSERT(G_N_ELEMENTS(datahistory_time_interval) == TIME_UNITS);

/*==========================================================================*
 * Implementation
 *==========================================================================*/

static GDateTime *datahistory_period_next(GDateTime *time,
				const struct datacounter_time_period *period)
{
	GDateTime *next;

	if (period->unit == TIME_UNIT_SECOND) {
		next = datacounters_time_add_period(time, period);
	} else {
		GTimeZone *utc = g_time_zone_new_utc();
		GDateTime *t1 = datacounters_time_add_period(time, period);

		next = datacounters_time_normalize(t1, utc, period->unit);
		g_time_zone_unref(utc);
		g_date_time_unref(t1);
	}
	return next;
}

static void datahistory_reset_cb(struct datacounter *counter, void *arg)
{
	datahistory_clear(DATAHISTORY(arg));
}

static void datahistory_update_cb(struct datacounter *dc,
	const struct connman_stats_data *change, GDateTime *time, void *arg)
{
	struct datahistory *self = DATAHISTORY(arg);
	struct datahistory_priv *priv = self->priv;
	const struct connman_stats_data *value = dc->value;

	if (g_date_time_compare(time, priv->next_period) < 0) {
		/* We are still within the same period */
		if (self->last_sample.bytes_sent != value->tx_bytes ||
			self->last_sample.bytes_received != value->rx_bytes) {
			/* The last sample has been updated */
			self->last_sample.time = g_date_time_to_unix(time);
			self->last_sample.bytes_sent = value->tx_bytes;
			self->last_sample.bytes_received = value->rx_bytes;
			g_signal_emit(self, datahistory_signal
					[SIGNAL_LAST_SAMPLE_CHANGED], 0);
		}
	} else {
		DataHistoryClass *klass = DATAHISTORY_GET_CLASS(self);
		struct datahistory_sample sample = self->last_sample;

		/*
		 * Archive the current sample, initialize the new one.
		 * Round the sample time to the next period boundary.
		 */
		sample.time = g_date_time_to_unix(priv->next_period);
		klass->push_sample(self, &sample);
		if (self->last_sample.time != sample.time) {
			self->last_sample.time = sample.time;
			g_signal_emit(self, datahistory_signal
				[SIGNAL_LAST_SAMPLE_CHANGED], 0);
		}

		/* Update the time when the next period starts */
		g_date_time_unref(priv->next_period);
		priv->next_period = datahistory_period_next(time,
							&self->type->period);
		DBG("next: %s", datacounter_format_time(dc,
							priv->next_period));

		/* Initialize the next sample */
		self->last_sample.time = g_date_time_to_unix(time);
		self->last_sample.bytes_sent = value->tx_bytes;
		self->last_sample.bytes_received = value->rx_bytes;

		/* Notify the listeners */
		g_signal_emit(self, datahistory_signal
				[SIGNAL_SAMPLE_ADDED], 0, &self->last_sample);
	}
}

/*==========================================================================*
 * API
 *==========================================================================*/

struct datahistory *datahistory_new(struct datacounter *dc,
				const struct datahistory_type *type)
{
	if (G_LIKELY(dc) && G_LIKELY(type)) {
		guint64 interval;
		struct datahistory *self = g_object_new(type->get_type(), NULL);
		struct datahistory_priv *priv = self->priv;
		DataHistoryClass *klass = DATAHISTORY_GET_CLASS(self);

		priv->start_time = datacounter_last_update_time(dc);
		if (priv->start_time) {
			g_date_time_ref(priv->start_time);
		} else {
			priv->start_time = datacounters_time_now();
		}
		priv->next_period = datahistory_period_next(priv->start_time,
							&type->period);
		DBG("next: %s", datacounter_format_time(dc,
						priv->next_period));
		self->counter = datacounter_ref(dc);
		self->start_time = g_date_time_to_unix(priv->start_time);
		self->type = type;
		self->name = type->name;
		self->last_sample.time =
			g_date_time_to_unix(priv->start_time);
		self->last_sample.bytes_sent = dc->value->tx_bytes;
		self->last_sample.bytes_received = dc->value->rx_bytes;
		priv->event_id[EVENT_RESET] =
			datacounter_add_reset_handler(dc,
						datahistory_reset_cb, self);
		priv->event_id[EVENT_UPDATE] =
			datacounter_add_update_handler(dc,
						datahistory_update_cb, self);
		/*
		 * Request periodic updates. The update interval is a worst
		 * case estimate, the actual updates may most likely happen
		 * more often than we have requested.
		 */
		interval = datahistory_time_interval[type->period.unit];
		interval *= type->period.value;
		if (interval < G_MAXUINT) {
			priv->update_interval = (guint)interval;
			__connman_rtnl_update_interval_add(
						priv->update_interval);
		}
		klass->finish_init(self);
		return self;
	}
	return NULL;
}

struct datahistory *datahistory_ref(struct datahistory *self)
{
	if (G_LIKELY(self)) {
		g_object_ref(DATAHISTORY(self));
	}
	return self;
}

void datahistory_unref(struct datahistory *self)
{
	if (G_LIKELY(self)) {
		g_object_unref(DATAHISTORY(self));
	}
}

void datahistory_add_to_idle_pool(struct datahistory *self,
				gpointer pointer, GDestroyNotify destroy)
{
	if (G_LIKELY(self)) {
		gutil_idle_pool_add(self->priv->pool, pointer, destroy);
	}
}

gboolean datahistory_persistent(struct datahistory *self)
{
	if (G_LIKELY(self)) {
		DataHistoryClass *klass = DATAHISTORY_GET_CLASS(self);
		return klass->persistent;
	}
	return FALSE;
}

void datahistory_clear(struct datahistory *self)
{
	if (G_LIKELY(self)) {
		struct datahistory_priv *priv = self->priv;
		DataHistoryClass *klass = DATAHISTORY_GET_CLASS(self);
		GDateTime *now = datacounters_time_now();
		const gint64 now_sec = g_date_time_to_unix(now);

		if (!klass->is_empty(self)) {
			klass->clear(self);
			g_signal_emit(self, datahistory_signal
						[SIGNAL_CLEARED], 0);
		}

		if (self->last_sample.time != now_sec ||
					self->last_sample.bytes_sent ||
					self->last_sample.bytes_received) {
			self->last_sample.time = now_sec;
			self->last_sample.bytes_sent = 0;
			self->last_sample.bytes_received = 0;
			g_signal_emit(self, datahistory_signal
					[SIGNAL_LAST_SAMPLE_CHANGED], 0);
		}

		if (!g_date_time_equal(now, priv->start_time)) {
			g_date_time_unref(priv->start_time);
			g_date_time_unref(priv->next_period);

			self->start_time = g_date_time_to_unix(now);
			priv->start_time = now;
			priv->next_period = datahistory_period_next(now,
							&self->type->period);
			DBG("next: %s", datacounter_format_time(self->counter,
							priv->next_period));

			g_signal_emit(self, datahistory_signal
					[SIGNAL_START_TIME_CHANGED], 0);
		} else {
			g_date_time_unref(now);
		}
	}
}

gboolean datahistory_get_sample_at_interval(struct datahistory *self,
			int interval, struct datahistory_sample *sample)
{
	if (G_LIKELY(self) && G_LIKELY(sample)) {
		/*
		 * The last sample is the current counter and therefore
		 * somewhat special. Its timestamp is the current time
		 * rather than the end of the interval.
		 *
		 * What it means is that samples 0 and 1 for history
		 * which counts every second would have the same timestamp.
		 * In other words, the end of the previous interval and
		 * the current time is the same thing. The caller has to
		 * be prepared for that.
		 */
		if (interval <= 0) {
			*sample = self->last_sample;
			return TRUE;
		} else {
			GDateTime *dt;
			gint64 max_time;
			struct datacounter_time_period tp = self->type->period;
			struct datahistory_priv *priv = self->priv;

			/* Move time back by specified number of intervals */
			tp.value *= -interval;
			dt = datahistory_period_next(priv->next_period, &tp);
			max_time = g_date_time_to_unix(dt);
			g_date_time_unref(dt);
			if (DATAHISTORY_GET_CLASS(self)->get_sample_at
						(self, max_time, sample)) {
				sample->time = max_time;
				return TRUE;
			}
		}
	}
	return FALSE;
}

const struct datahistory_samples *datahistory_get_samples
				(struct datahistory *self, int max_count)
{
	if (G_LIKELY(self)) {
		return DATAHISTORY_GET_CLASS(self)->get_samples
						(self, max_count);
	}
	return NULL;
}

const struct datahistory_samples *datahistory_get_samples_since(
			struct datahistory *self, gint64 since, int max_count)
{
	if (G_LIKELY(self)) {
		return DATAHISTORY_GET_CLASS(self)->get_samples_since
						(self, since, max_count);
	}
	return NULL;
}

gulong datahistory_add_cleared_handler(struct datahistory *self,
				datahistory_cb_t cb, void *arg)
{
	return (G_LIKELY(self) && G_LIKELY(cb)) ? g_signal_connect(self,
		SIGNAL_CLEARED_NAME, G_CALLBACK(cb), arg) : 0;
}

gulong datahistory_add_start_time_handler(struct datahistory *self,
				datahistory_cb_t cb, void *arg)
{
	return (G_LIKELY(self) && G_LIKELY(cb)) ? g_signal_connect(self,
		SIGNAL_START_TIME_CHANGED_NAME, G_CALLBACK(cb), arg) : 0;
}

gulong datahistory_add_last_sample_handler(struct datahistory *self,
				datahistory_cb_t cb, void *arg)
{
	return (G_LIKELY(self) && G_LIKELY(cb)) ? g_signal_connect(self,
		SIGNAL_LAST_SAMPLE_CHANGED_NAME, G_CALLBACK(cb), arg) : 0;
}

gulong datahistory_add_sample_added_handler(struct datahistory *self,
				datahistory_sample_cb_t cb, void *arg)
{
	return (G_LIKELY(self) && G_LIKELY(cb)) ? g_signal_connect(self,
		SIGNAL_SAMPLE_ADDED_NAME, G_CALLBACK(cb), arg) : 0;
}

void datahistory_remove_handler(struct datahistory *self, gulong id)
{
	if (G_LIKELY(self) && G_LIKELY(id)) {
		g_signal_handler_disconnect(self, id);
	}
}

void datahistory_remove_handlers(struct datahistory *self, gulong *ids,
								guint count)
{
	gutil_disconnect_handlers(self, ids, count);
}

/*==========================================================================*
 * Internals
 *==========================================================================*/

static void datahistory_init(struct datahistory *self)
{
	struct datahistory_priv *priv = G_TYPE_INSTANCE_GET_PRIVATE(self,
				DATAHISTORY_TYPE, struct datahistory_priv);

	self->priv = priv;
	priv->pool = gutil_idle_pool_new();
}

static void datahistory_finalize(GObject *object)
{
	struct datahistory *self = DATAHISTORY(object);
	struct datahistory_priv *priv = self->priv;
	datacounter_remove_all_handlers(self->counter, priv->event_id);
	datacounter_unref(self->counter);
	gutil_idle_pool_unref(priv->pool);
	g_date_time_unref(priv->start_time);
	g_date_time_unref(priv->next_period);
	/* Zero interval is ignored */
	__connman_rtnl_update_interval_remove(priv->update_interval);
	G_OBJECT_CLASS(PARENT_CLASS)->finalize(object);
}

static void datahistory_class_init(DataHistoryClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS(klass);
	object_class->finalize = datahistory_finalize;
	g_type_class_add_private(klass, sizeof(struct datahistory_priv));
	datahistory_signal[SIGNAL_CLEARED] =
		g_signal_new(SIGNAL_CLEARED_NAME, G_OBJECT_CLASS_TYPE(klass),
			G_SIGNAL_RUN_FIRST, 0, NULL, NULL, NULL,
			G_TYPE_NONE, 0);
	datahistory_signal[SIGNAL_START_TIME_CHANGED] =
		g_signal_new(SIGNAL_START_TIME_CHANGED_NAME,
			G_OBJECT_CLASS_TYPE(klass),
			G_SIGNAL_RUN_FIRST, 0, NULL, NULL, NULL,
			G_TYPE_NONE, 0);
	datahistory_signal[SIGNAL_SAMPLE_ADDED] =
		g_signal_new(SIGNAL_SAMPLE_ADDED_NAME,
			G_OBJECT_CLASS_TYPE(klass),
			G_SIGNAL_RUN_FIRST, 0, NULL, NULL, NULL,
			G_TYPE_NONE, 1, G_TYPE_POINTER);
	datahistory_signal[SIGNAL_LAST_SAMPLE_CHANGED] =
		g_signal_new(SIGNAL_LAST_SAMPLE_CHANGED_NAME,
			G_OBJECT_CLASS_TYPE(klass),
			G_SIGNAL_RUN_FIRST, 0, NULL, NULL, NULL,
			G_TYPE_NONE, 0);
}

/*
 * Local Variables:
 * mode: C
 * c-basic-offset: 8
 * indent-tabs-mode: t
 * End:
 */