Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
timeshift: add memoryinfo support
  • Loading branch information
perexg committed Nov 13, 2016
1 parent 3456fd1 commit 2411090
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/timeshift.c
Expand Up @@ -38,6 +38,9 @@ static int timeshift_index = 0;

struct timeshift_conf timeshift_conf;

memoryinfo_t timeshift_memoryinfo = { .my_name = "Timeshift" };
memoryinfo_t timeshift_memoryinfo_ram = { .my_name = "Timeshift RAM buffer" };

/*
* Packet log
*/
Expand Down Expand Up @@ -75,6 +78,9 @@ void timeshift_init ( void )
{
htsmsg_t *m;

memoryinfo_register(&timeshift_memoryinfo);
memoryinfo_register(&timeshift_memoryinfo_ram);

timeshift_filemgr_init();

/* Defaults */
Expand All @@ -101,6 +107,9 @@ void timeshift_term ( void )
timeshift_filemgr_term();
free(timeshift_conf.path);
timeshift_conf.path = NULL;

memoryinfo_unregister(&timeshift_memoryinfo);
memoryinfo_unregister(&timeshift_memoryinfo_ram);
}

/*
Expand Down Expand Up @@ -418,7 +427,9 @@ timeshift_destroy(streaming_target_t *pad)

if (ts->path)
free(ts->path);

free(ts);
memoryinfo_free(&timeshift_memoryinfo, sizeof(timeshift_t));
}

/**
Expand All @@ -432,6 +443,8 @@ streaming_target_t *timeshift_create
{
timeshift_t *ts = calloc(1, sizeof(timeshift_t));

memoryinfo_alloc(&timeshift_memoryinfo, sizeof(timeshift_t));

/* Must hold global lock */
lock_assert(&global_lock);

Expand Down
4 changes: 4 additions & 0 deletions src/timeshift.h
Expand Up @@ -20,6 +20,7 @@
#define __TVH_TIMESHIFT_H__

#include "idnode.h"
#include "memoryinfo.h"

typedef struct timeshift_conf {
idnode_t idnode;
Expand All @@ -41,6 +42,9 @@ typedef struct timeshift_conf {
extern struct timeshift_conf timeshift_conf;
extern const idclass_t timeshift_conf_class;

extern memoryinfo_t timeshift_memoryinfo;
extern memoryinfo_t timeshift_memoryinfo_ram;

void timeshift_init ( void );
void timeshift_term ( void );

Expand Down
10 changes: 9 additions & 1 deletion src/timeshift/timeshift_filemgr.c
@@ -1,4 +1,4 @@
/**
/*
* TV headend - Timeshift File Manager
* Copyright (C) 2012 Adam Sutton
*
Expand Down Expand Up @@ -81,16 +81,20 @@ static void* timeshift_reaper_callback ( void *p )
/* Free memory */
while ((ti = TAILQ_FIRST(&tsf->iframes))) {
TAILQ_REMOVE(&tsf->iframes, ti, link);
memoryinfo_free(&timeshift_memoryinfo, sizeof(*ti));
free(ti);
}
while ((tid = TAILQ_FIRST(&tsf->sstart))) {
TAILQ_REMOVE(&tsf->sstart, tid, link);
sm = tid->data;
streaming_msg_free(sm);
memoryinfo_free(&timeshift_memoryinfo, sizeof(*tid));
free(tid);
}
free(tsf->path);
memoryinfo_free(&timeshift_memoryinfo_ram, tsf->ram_size);
free(tsf->ram);
memoryinfo_free(&timeshift_memoryinfo, sizeof(*tsf));
free(tsf);

pthread_mutex_lock(&timeshift_reaper_lock);
Expand Down Expand Up @@ -178,6 +182,7 @@ void timeshift_filemgr_close ( timeshift_file_t *tsf )
/* maintain unused memory block */
ram = realloc(tsf->ram, tsf->woff);
if (ram) {
memoryinfo_append(&timeshift_memoryinfo_ram, tsf->ram_size - tsf->woff);
tsf->ram = ram;
tsf->ram_size = tsf->woff;
}
Expand Down Expand Up @@ -238,6 +243,7 @@ static timeshift_file_t * timeshift_filemgr_file_init
timeshift_file_t *tsf;

tsf = calloc(1, sizeof(timeshift_file_t));
memoryinfo_alloc(&timeshift_memoryinfo, sizeof(*tsf));
tsf->time = mono2sec(start_time) / TIMESHIFT_FILE_PERIOD;
tsf->last = start_time;
tsf->wfd = -1;
Expand Down Expand Up @@ -331,6 +337,7 @@ timeshift_file_t *timeshift_filemgr_get ( timeshift_t *ts, int64_t start_time )
tvhtrace(LS_TIMESHIFT, "ts %d create RAM segment with %"PRId64" bytes (time %"PRId64")",
ts->id, tsf_tmp->ram_size, start_time);
ts->ram_segments++;
memoryinfo_alloc(&timeshift_memoryinfo_ram, tsf_tmp->ram_size);
}
break;
} else {
Expand Down Expand Up @@ -371,6 +378,7 @@ timeshift_file_t *timeshift_filemgr_get ( timeshift_t *ts, int64_t start_time )
ts->id, ti ? " (from last file)" : "");
timeshift_index_data_t *ti2 = calloc(1, sizeof(timeshift_index_data_t));
if (ti) {
memoryinfo_alloc(&timeshift_memoryinfo, sizeof(timeshift_index_data_t));
sm = streaming_msg_clone(ti->data);
} else {
sm = streaming_msg_create(SMT_START);
Expand Down
3 changes: 3 additions & 0 deletions src/timeshift/timeshift_writer.c
Expand Up @@ -73,6 +73,7 @@ static ssize_t _write
pthread_mutex_unlock(&tsf->ram_lock);
return -1;
}
memoryinfo_append(&timeshift_memoryinfo_ram, alloc);
tsf->ram = ram;
tsf->ram_size += alloc;
}
Expand Down Expand Up @@ -257,6 +258,7 @@ static void _handle_sstart ( timeshift_t *ts, timeshift_file_t *tsf, streaming_m
{
timeshift_index_data_t *ti = calloc(1, sizeof(timeshift_index_data_t));

memoryinfo_alloc(&timeshift_memoryinfo, sizeof(*ti));
ti->pos = tsf->size;
ti->data = sm;
TAILQ_INSERT_TAIL(&tsf->sstart, ti, link);
Expand Down Expand Up @@ -285,6 +287,7 @@ static inline ssize_t _process_msg0
if (pkt->pkt_componentindex == ts->vididx &&
pkt->pkt_frametype == PKT_I_FRAME) {
timeshift_index_iframe_t *ti = calloc(1, sizeof(timeshift_index_iframe_t));
memoryinfo_alloc(&timeshift_memoryinfo, sizeof(*ti));
ti->pos = tsf->size;
ti->time = sm->sm_time;
TAILQ_INSERT_TAIL(&tsf->iframes, ti, link);
Expand Down

0 comments on commit 2411090

Please sign in to comment.