aboutsummaryrefslogtreecommitdiffstats
path: root/trace-input.c
diff options
context:
space:
mode:
Diffstat (limited to 'trace-input.c')
-rw-r--r--trace-input.c97
1 files changed, 95 insertions, 2 deletions
diff --git a/trace-input.c b/trace-input.c
index 1462b4b..dc1167a 100644
--- a/trace-input.c
+++ b/trace-input.c
@@ -52,6 +52,7 @@ struct tracecmd_input {
52 int page_size; 52 int page_size;
53 int read_page; 53 int read_page;
54 int cpus; 54 int cpus;
55 int ref;
55 struct cpu_data *cpu_data; 56 struct cpu_data *cpu_data;
56 57
57 /* file information */ 58 /* file information */
@@ -1470,10 +1471,20 @@ void tracecmd_print_events(struct tracecmd_input *handle)
1470} 1471}
1471 1472
1472/** 1473/**
1473 * tracecmd_open_fd - create a tracecmd_handle from the trace.dat file descriptor 1474 * tracecmd_alloc_fd - create a tracecmd_input handle from a file descriptor
1474 * @fd: the file descriptor for the trace.dat file 1475 * @fd: the file descriptor for the trace.dat file
1476 *
1477 * Allocate a tracecmd_input handle from a file descriptor and open the
1478 * file. This tests if the file is of trace-cmd format and allocates
1479 * a parse event descriptor.
1480 *
1481 * The returned pointer is not ready to be read yet. A tracecmd_read_headers()
1482 * and tracecmd_init_data() still need to be called on the descriptor.
1483 *
1484 * Unless you know what you are doing with this, you want to use
1485 * tracecmd_open_fd() instead.
1475 */ 1486 */
1476struct tracecmd_input *tracecmd_open_fd(int fd) 1487struct tracecmd_input *tracecmd_alloc_fd(int fd)
1477{ 1488{
1478 struct tracecmd_input *handle; 1489 struct tracecmd_input *handle;
1479 char test[] = { 23, 8, 68 }; 1490 char test[] = { 23, 8, 68 };
@@ -1486,6 +1497,7 @@ struct tracecmd_input *tracecmd_open_fd(int fd)
1486 memset(handle, 0, sizeof(*handle)); 1497 memset(handle, 0, sizeof(*handle));
1487 1498
1488 handle->fd = fd; 1499 handle->fd = fd;
1500 handle->ref = 1;
1489 1501
1490 if (do_read_check(handle, buf, 3)) 1502 if (do_read_check(handle, buf, 3))
1491 goto failed_read; 1503 goto failed_read;
@@ -1531,6 +1543,56 @@ struct tracecmd_input *tracecmd_open_fd(int fd)
1531} 1543}
1532 1544
1533/** 1545/**
1546 * tracecmd_alloc_fd - create a tracecmd_input handle from a file name
1547 * @file: the file name of the file that is of tracecmd data type.
1548 *
1549 * Allocate a tracecmd_input handle from a given file name and open the
1550 * file. This tests if the file is of trace-cmd format and allocates
1551 * a parse event descriptor.
1552 *
1553 * The returned pointer is not ready to be read yet. A tracecmd_read_headers()
1554 * and tracecmd_init_data() still need to be called on the descriptor.
1555 *
1556 * Unless you know what you are doing with this, you want to use
1557 * tracecmd_open() instead.
1558 */
1559struct tracecmd_input *tracecmd_alloc(const char *file)
1560{
1561 int fd;
1562
1563 fd = open(file, O_RDONLY);
1564 if (fd < 0)
1565 return NULL;
1566
1567 return tracecmd_alloc_fd(fd);
1568}
1569
1570/**
1571 * tracecmd_open_fd - create a tracecmd_handle from the trace.dat file descriptor
1572 * @fd: the file descriptor for the trace.dat file
1573 */
1574struct tracecmd_input *tracecmd_open_fd(int fd)
1575{
1576 struct tracecmd_input *handle;
1577
1578 handle = tracecmd_alloc_fd(fd);
1579 if (!handle)
1580 return NULL;
1581
1582 if (tracecmd_read_headers(handle) < 0)
1583 goto fail;
1584
1585 if (tracecmd_init_data(handle) < 0)
1586 goto fail;
1587
1588 return handle;
1589
1590fail:
1591 tracecmd_close(handle);
1592 return NULL;
1593}
1594
1595/**
1534 * tracecmd_open - create a tracecmd_handle from a given file 1596 * tracecmd_open - create a tracecmd_handle from a given file
1535 * @file: the file name of the file that is of tracecmd data type. 1597 * @file: the file name of the file that is of tracecmd data type.
1536 */ 1598 */
@@ -1545,6 +1607,29 @@ struct tracecmd_input *tracecmd_open(const char *file)
1545 return tracecmd_open_fd(fd); 1607 return tracecmd_open_fd(fd);
1546} 1608}
1547 1609
1610/**
1611 * tracecmd_ref - add a reference to the handle
1612 * @handle: input handle for the trace.dat file
1613 *
1614 * Some applications may share a handle between parts of
1615 * the application. Let those parts add reference counters
1616 * to the handle, and the last one to close it will free it.
1617 */
1618void tracecmd_ref(struct tracecmd_input *handle)
1619{
1620 if (!handle)
1621 return;
1622
1623 handle->ref++;
1624}
1625
1626/**
1627 * tracecmd_close - close and free the trace.dat handle
1628 * @handle: input handle for the trace.dat file
1629 *
1630 * Close the file descriptor of the handle and frees
1631 * the resources allocated by the handle.
1632 */
1548void tracecmd_close(struct tracecmd_input *handle) 1633void tracecmd_close(struct tracecmd_input *handle)
1549{ 1634{
1550 int cpu; 1635 int cpu;
@@ -1552,6 +1637,14 @@ void tracecmd_close(struct tracecmd_input *handle)
1552 if (!handle) 1637 if (!handle)
1553 return; 1638 return;
1554 1639
1640 if (handle->ref <= 0) {
1641 warning("tracecmd: bad ref count on handle\n");
1642 return;
1643 }
1644
1645 if (--handle->ref)
1646 return;
1647
1555 for (cpu = 0; cpu < handle->cpus; cpu++) { 1648 for (cpu = 0; cpu < handle->cpus; cpu++) {
1556 struct record *rec; 1649 struct record *rec;
1557 /* 1650 /*