# This scripts checks dtrace probes provided by mysqld using real dtrace tool
# and systemtap.
--source include/not_windows.inc

--let OUTPUT_FILENAME= $MYSQLTEST_VARDIR/tmp/output.txt
--let MYSQLD_PIDFILE= `SELECT @@pid_file`
--let MYSQLD= $MYSQLD
--let SCRIPT_OUTPUT_FILE= $MYSQLTEST_VARDIR/tmp/script_output.txt

--perl
  my $pid_filename = $ENV{'MYSQLD_PIDFILE'};
  my $output_filename = $ENV{'OUTPUT_FILENAME'};
  my $mysqld = $ENV{'MYSQLD'};

  my $mysqld_pid = 0;
  my $dyn_trace_tool_available = 0;
  my $user_has_permissions = 0;
  my $mysqld_has_dtrace_probes = 0;
  my $command = "";
  my $ret;

# Check whether Dtrace tool exists or not. If exists then capture its output.
  $ret = `dtrace -V 2>&1`;
  if ($? == 0)
  {
    if (($ret =~ m/Sun D/) || ($ret =~ m/Oracle D/))
    {
      $dyn_trace_tool_available = 1;

      # Check whether user has Dtrace tool execution permission or not	
      $ret = `dtrace -v 2>&1`;
      if (!($ret =~ m/DTrace requires additional privileges/))		
      {
        $user_has_permissions = 1;

        $mysqld_pid= `cat $pid_filename`; 
        chomp($mysqld_pid); 		

        # Check whether MySQL Dtrace providers exists or not.
        $ret=`dtrace -l -p $mysqld_pid |
          egrep -ci \"net-read-done|connection-start|query-parse-start\"`;
        if ($ret != 0)
        {
          $mysqld_has_dtrace_probes = 1;
          $command = "dtrace -s \$MYSQL_TEST_DIR/std_data/dtrace.d ";
        }
      }
    }
  }
  else
  {
    # check whether stap tool exists or not.
    $ret= `stap 2>&1`;
    if ($ret == 0)
    {
      $dyn_trace_tool_available= 1;

      # Check whether user has permission to run stap.
      $ret= `stap -e 'probe process("ls").function("main") { exit() }'
             -c ls 2>&1|egrep -ic "You should be part of the group \"stapusr"`; 

      # Check whether user has permission to run staprun or not. 
      if ($ret == 0)
      {
        $ret= `staprun 2>&1|egrep -ic "Permission denied"`;
      }
      if ($ret == 0)
      {
        $user_has_permissions= 1;

        # Check whether MySQL stap markers are available or not.
        $ret= `stap -L 'process("$mysqld").mark("*")'|
          egrep -ci \"net__read__done|connection__start|query__parse__start\"`; 
          if ($ret != 0)
          {
            $mysqld_has_dtrace_probes = 1;
            $command= "stap \$MYSQL_TEST_DIR/std_data/system_tap.stp $mysqld ";
          }
      }
    }
  }

  # Write output to file.
  open(FILE, ">> $output_filename");
  print FILE "--let \$DYN_TRACE_TOOL_AVAILABLE= $dyn_trace_tool_available\n";
  print FILE "--let \$USER_HAS_PERMISSIONS= $user_has_permissions\n";
  print FILE "--let \$MYSQLD_HAS_DTRACE_PROBES= $mysqld_has_dtrace_probes\n";
  print FILE "--let COMMAND= $command\n";
  close(FILE);
EOF

source $OUTPUT_FILENAME;

if (!$DYN_TRACE_TOOL_AVAILABLE)
{
  --skip Real DTrace or System tap tool is required to run this test.
}

if (!$USER_HAS_PERMISSIONS)
{
  --skip dtrace/stap tool requires additional privileges to run this test.
}

if (!$MYSQLD_HAS_DTRACE_PROBES)
{
  --skip MySQLD does not have MySQL probes.
}

--disable_result_log
DELIMITER $;

CREATE PROCEDURE create_table_and_insert_rows()
BEGIN
  DECLARE count INT;
  SET count = 1;

  SELECT SLEEP(10);
  CREATE TABLE t1 (f1 INT);
  INSERT INTO t1 VALUES (7894);
  WHILE count <= 5 DO
    INSERT INTO t1 SELECT * FROM t1;
    SET count = count + 1;
  END WHILE;

  SET count = 1;
  WHILE count <= 10000 DO
    SELECT SQL_NO_CACHE count(*) from t1;
    SET count = count + 1;
  END WHILE;
END
$

DELIMITER ;$

# Running Dtrace script in background.
--perl
   system("$ENV{'COMMAND'} >$ENV{'SCRIPT_OUTPUT_FILE'} 2>&1 &"); 
EOF

CALL create_table_and_insert_rows();
# Waiting for few secs to allow Dtrace to write all trace output to file.
SELECT SLEEP(4);
--enable_result_log

# Dtrace output
--exec cat $SCRIPT_OUTPUT_FILE

# Cleanup
--remove_file $OUTPUT_FILENAME
--remove_file $SCRIPT_OUTPUT_FILE
DROP TABLE t1;
DROP PROCEDURE create_table_and_insert_rows;
