uprofiler_param_init($params);
if (!empty($run)) {
    // single run mode
    $raw_data = $uprofiler_runs_impl->get_run($run, $source, $desc_unused);
    $functions = uprofiler_get_matching_functions($q, $raw_data);
} else {
    if (!empty($run1) && !empty($run2)) {
        // diff mode
        $raw_data = $uprofiler_runs_impl->get_run($run1, $source, $desc_unused);
        $functions1 = uprofiler_get_matching_functions($q, $raw_data);
        $raw_data = $uprofiler_runs_impl->get_run($run2, $source, $desc_unused);
        $functions2 = uprofiler_get_matching_functions($q, $raw_data);
        $functions = array_unique(array_merge($functions1, $functions2));
        asort($functions);
    } else {
        uprofiler_error("no valid runs specified to typeahead endpoint");
        $functions = array();
    }
}
// If exact match is present move it to the front
if (in_array($q, $functions)) {
    $old_functions = $functions;
    $functions = array($q);
    foreach ($old_functions as $f) {
        // exact match case has already been added to the front
        if ($f != $q) {
            $functions[] = $f;
        }
    }
}
foreach ($functions as $f) {
/**
 * Initialize params from URL query string. The function
 * creates globals variables for each of the params
 * and if the URL query string doesn't specify a particular
 * param initializes them with the corresponding default
 * value specified in the input.
 *
 * @params array $params An array whose keys are the names
 *                       of URL params who value needs to
 *                       be retrieved from the URL query
 *                       string. PHP globals are created
 *                       with these names. The value is
 *                       itself an array with 2-elems (the
 *                       param type, and its default value).
 *                       If a param is not specified in the
 *                       query string the default value is
 *                       used.
 * @author Kannan
 */
function uprofiler_param_init($params)
{
    /* Create variables specified in $params keys, init defaults */
    foreach ($params as $k => $v) {
        switch ($v[0]) {
            case UPROFILER_STRING_PARAM:
                $p = uprofiler_get_string_param($k, $v[1]);
                break;
            case UPROFILER_UINT_PARAM:
                $p = uprofiler_get_uint_param($k, $v[1]);
                break;
            case UPROFILER_FLOAT_PARAM:
                $p = uprofiler_get_float_param($k, $v[1]);
                break;
            case UPROFILER_BOOL_PARAM:
                $p = uprofiler_get_bool_param($k, $v[1]);
                break;
            default:
                uprofiler_error("Invalid param type passed to uprofiler_param_init: " . $v[0]);
                exit;
        }
        if ($k === 'run') {
            $p = implode(',', array_filter(explode(',', $p), 'ctype_xdigit'));
        }
        // create a global variable using the parameter name.
        $GLOBALS[$k] = $p;
    }
}
 public function save_run($uprofiler_data, $type, $run_id = null)
 {
     // Use PHP serialize function to store the uprofiler's
     // raw profiler data.
     $uprofiler_data = serialize($uprofiler_data);
     if ($run_id === null) {
         $run_id = $this->gen_run_id($type);
     }
     $file_name = $this->file_name($run_id, $type);
     $file = fopen($file_name, 'w');
     if ($file) {
         fwrite($file, $uprofiler_data);
         fclose($file);
     } else {
         uprofiler_error("Could not open {$file_name}\n");
     }
     // echo "Saved run in {$file_name}.\nRun id = {$run_id}.\n";
     return $run_id;
 }
/**
 * Generate image content from phprof run id.
 *
 * @param object  $uprofiler_runs_impl  An object that implements
 *                                   the iUprofilerRuns interface
 * @param run_id, integer, the unique id for the phprof run, this is the
 *                primary key for phprof database table.
 * @param type, string, one of the supported image types. See also
 *              $uprofiler_legal_image_types.
 * @param threshold, float, the threshold value [0,1). The functions in the
 *                   raw_data whose exclusive wall times ratio are below the
 *                   threshold will be filtered out and won't apprear in the
 *                   generated image.
 * @param func, string, the focus function.
 * @returns, string, the DOT script to generate image.
 *
 * @author cjiang
 */
function uprofiler_get_content_by_run($uprofiler_runs_impl, $run_id, $type, $threshold, $func, $source, $critical_path)
{
    if (!$run_id) {
        return "";
    }
    $raw_data = $uprofiler_runs_impl->get_run($run_id, $source, $description);
    if (!$raw_data) {
        uprofiler_error("Raw data is empty");
        return "";
    }
    $script = uprofiler_generate_dot_script($raw_data, $threshold, $source, $description, $func, $critical_path);
    $content = uprofiler_generate_image_by_dot($script, $type);
    return $content;
}