int
generate_lists_of_files(const char* tmp_file, long newrelic_transaction)
{
    long newr_segm_external_find = 0;
    if (newrelic_transaction >= 0) {
        newr_segm_external_find =
                newrelic_segment_external_begin(newrelic_transaction,
                                                NEWRELIC_ROOT_SEGMENT,
                                                "localhost", "find");
        if (newr_segm_external_find < 0)
            fprintf(stderr, "ERROR: newrelic_segment_external_begin() "
                            "returned %ld\n", newr_segm_external_find);
    }

    char external_command[32*1024];
    snprintf(external_command, sizeof external_command,
             "/usr/bin/find  / -type f -perm -100 -printf '%%s %%p\\n' "
             "> %s 2>/dev/null", tmp_file);

    fprintf(stderr, "Running: %s\n", external_command);

    int ext_cmd_ret = system(external_command);

    /* Just after running the "external_command" (find / -perm -100 ...)
     * record two custom measures up to NewRelic, as an example.
     * Ie., how busy was the cache left.
     * TODO: the load left on the VM/disks. But the disks and
     * filesystems can be different, so this is not an simple
     * task for this NewRelic SDK test.
     */

     record_newrelic_metric_from_first_value_of_kernel_stat_file(
            "/proc/sys/fs/dentry-state",
            "Custom/ct_cached_dentries_metric"
          );

     record_newrelic_metric_from_first_value_of_kernel_stat_file(
            "/proc/sys/fs/inode-state",
            "Custom/ct_cached_inodes_metric"
          );

    /* end this NewRelic segment */
    if (newrelic_transaction >= 0 && newr_segm_external_find >= 0) {
        int ret_code =  newrelic_segment_end(newrelic_transaction,
                                             newr_segm_external_find);
        if (ret_code < 0)
            fprintf(stderr, "ERROR: newrelic_segment_end() returned %d\n",
                    ret_code);
    }

    /* The `find` command will exit non-zero if it doesn't have permission
     * to enter into a subdirectory, and similar cases with permissions
     * -that is why the `find` command above is executed with '2>/dev/null'.
     * So WEXITSTATUS(ext_cmd_ret), for the `find` case, may return errors
     * which the user normally ignores.
     */

    return ( ext_cmd_ret != -1 ) ?  0: -1;
}
    void NewRelicProfiler::endFrameEx(const TypedValue *retval,
    const char *given_symbol) {

        char symbol[512];
        NewRelicProfilerFrame *frame = dynamic_cast<NewRelicProfilerFrame *>(m_stack);
        frame->getStack(2, symbol, sizeof(symbol));

        if (frame->m_nr_segement_code > 0) {
            newrelic_segment_end(NEWRELIC_AUTOSCOPE, frame->m_nr_segement_code);
            frame->m_nr_segement_code = 0;
        }
    }
int
process_lists_of_files(const char* tmp_file, long newrelic_transaction,
                       long* max_exec_size_found, char* max_exec_fname)
{
    long newr_segm_internal_proc = 0;
    if (newrelic_transaction >= 0) {
        newr_segm_internal_proc =
                newrelic_segment_generic_begin(newrelic_transaction,
                                              NEWRELIC_ROOT_SEGMENT,
                                           "processing_list_execs");
        if (newr_segm_internal_proc < 0)
            fprintf(stderr, "ERROR: newrelic_segment_external_begin() "
                            "returned %ld\n", newr_segm_internal_proc);
    }

    long curr_max_fsize = -1;
    char curr_max_fname[PATH_MAX];

    FILE *f;
    f = fopen(tmp_file, "r");
    if (f) {
        char line[PATH_MAX+32];
        long fsize;
        char fname[PATH_MAX];
        long numb_lines = 0;

        while (fgets(line, sizeof line, f) != NULL) {
             sscanf (line, "%ld %s", &fsize, fname);
             if (fsize > curr_max_fsize) {
                 curr_max_fsize = fsize;
                 strncpy(curr_max_fname, fname, sizeof curr_max_fname);
             }
             numb_lines++;  /* one line per file, as per output of `find` */
        }

        fclose(f);

        if (newrelic_transaction >= 0) {
            char total_numb_executables[32];
            snprintf(total_numb_executables, sizeof total_numb_executables,
                     "%ld", numb_lines);
            int ret_code =
                 newrelic_transaction_add_attribute(newrelic_transaction,
                                                "Custom/ct_NUMBER_EXECUTABLES",
                                                total_numb_executables);
            if (ret_code < 0)
                fprintf(stderr, "ERROR: newrelic_transaction_add_attribute() "
                                "returned %d\n", ret_code);
        }
    } else
        curr_max_fname[0] = '\0';

    *max_exec_size_found = curr_max_fsize;
    strncpy(max_exec_fname, curr_max_fname, sizeof curr_max_fname);

    if (newrelic_transaction >= 0 && newr_segm_internal_proc >= 0) {
        int ret_code =  newrelic_segment_end(newrelic_transaction,
                                             newr_segm_internal_proc);
        if (ret_code < 0)
            fprintf(stderr, "ERROR: newrelic_segment_end() returned %d\n",
                    ret_code);
    }

    return (curr_max_fname[0] != 0) ? 0: -1 ;
}