Jak szybki jest grep? -> grep vs awk, python, rugby, java, perl, C - przeszukiwanie logów - część 8.




Kontynuacja kodu z części siódmej.
C++ - ram

  process[maxproc - 1].shm_len =
    (fileinmemory + len_max) - process[maxproc - 1].shm_start;
  printf ("\n");
  for (i = 0; i < maxproc; ++i)
    {
      process[i].shm_diff = process[i].shm_start - fileinmemory;
    }
  adressum = 0;
  for (i = 0; i < maxproc; ++i)
    {
      printf ("%2d: shm_start: %p ", i, process[i].shm_start);
      printf ("  shm_len: %lX ", process[i].shm_len);
      printf ("  shm_diff: %8lX ", process[i].shm_diff);
      adressum += process[i].shm_len;
      printf ("  shm_start+shm_len: %lX \n",
          process[i].shm_len + (long int) process[i].shm_start);
    }
  printf ("Addr len_max: %lX \n", len_max);
  printf ("    Diff sum: %lX \n", adressum);
  printf ("\n");
  sigemptyset (&sig_mask);
  sigfillset (&sig_mask);
  sigA.sa_handler = NULL;
  sigA.sa_sigaction = sygnal;
  sigA.sa_mask = sig_mask;
  sigA.sa_flags = SA_SIGINFO;
  sigA.sa_restorer = NULL;
  if (sigaction (SIGCHLD, &sigA, NULL))
    {
      perror ("Blad sigaction:");
      exit (33);
    }
  cout << "-------------------------- Przed fork:" << (double) chrono::
    duration_cast < chrono::microseconds >
    (chrono::high_resolution_clock::now () -
     timeTestStart).count () / 1000000 << "s" << endl;
  for (int i = 0; i < maxproc; i++)
    {
      process[i].countTimeProcess_start = times (&bufor);
      process[i].pid = fork ();
      switch (process[i].pid)
    {
    case -1:
      cout << "Nie mozna utworzyc nowego procesu nr: " << i << endl;
      break;
    case 0:
      {
        char needlechild_extern[10];
        strcpy (needlechild_extern, argv[1]);
        exit (child_extern
          (i, smid, process[i].shm_diff,
           (unsigned long) process[i].shm_len, needlechild_extern));
        break;
      }
    default:
      break;
    }
    }
  cout << "-------------------------- Po fork w main:" << (double) chrono::
    duration_cast < chrono::microseconds >
    (chrono::high_resolution_clock::now () -
     timeTestStart).count () / 1000000 << "s" << endl;
  do
    {
      pause ();
    }
  while (end_main < (maxproc - 1));
  cout << "-------------------------- Po wait na end_main:" << (double)
    chrono::duration_cast < chrono::microseconds >
    (chrono::high_resolution_clock::now () -
     timeTestStart).count () / 1000000 << "s" << endl;
  // uzywam wait pokazujac zachowanie sie tablicy stanu procesu po jego
  // zakonczeniu
  // te dzialania mozna przeniesc do funkcji obslugujacej sygnal
  // i odpytywac sie tam o stan procesu, ktory wlasnie wyslal sysgnal
  // wait3 == wait4(0,...)
  while ((pid_tmp = wait4 (0, &returnCode, 0, &timeData)) != (-1))
    {
      for (int i = 0; i < maxproc; i++)
    {
      if (process[i].pid == pid_tmp)
        {
          process[i].timeProcSystem_us = timeData.ru_stime.tv_usec;
          process[i].timeProcUser_us = timeData.ru_utime.tv_usec;
          process[i].returnCode = returnCode;
          i = maxproc;
        }
    }
    }
  cout << "-------------------------- Przed write:" << (double) chrono::
    duration_cast < chrono::microseconds >
    (chrono::high_resolution_clock::now () -
     timeTestStart).count () / 1000000 << "s" << endl;
  int *len_buff;
  fstream file_out;
  file_out.open ("wynik_multifind.txt", ios::out);
  if (file_out.good () == true)
    {
      for (int i = 0; i < maxproc; ++i)
    {
      len_buff =
        (int *) (fileinmemory + process[i].shm_diff + process[i].shm_len -
             8);
      file_out.write (process[i].shm_start, *len_buff);
    }
      file_out.close ();
    }
  cout << "-------------------------- Po write:" << (double) chrono::
    duration_cast < chrono::microseconds >
    (chrono::high_resolution_clock::now () -
     timeTestStart).count () / 1000000 << "s" << endl;
  for (int i = 0; i < maxproc; ++i)
    {
      cout << "*** Proces: [" << i << "], kod zakonczenia: " << (process[i].
                                 returnCode >>
                                 8) << endl;
      cout << "Czas pracy procesu: " << (double) (process[i].
                          countTimeProcess_stop -
                          process[i].
                          countTimeProcess_start) /
    sysconf (_SC_CLK_TCK) << "s" << endl;
      cout << "Czas procesora w trybie uzytkownika: " << setw (8) << (double)
    process[i].timeProcUser_us / CLOCKS_PER_SEC << "s" << endl;
      cout << "Czas procesora w trybie systemowym: " << setw (9) << (double)
    process[i].timeProcSystem_us / CLOCKS_PER_SEC << "s" << endl << endl;
    }
  cout << "-------------------------- Przed shmdt:" << (double) chrono::
    duration_cast < chrono::microseconds >
    (chrono::high_resolution_clock::now () -
     timeTestStart).count () / 1000000 << "s" << endl;
  int r = shmdt (fileinmemory);
  if (r == -1)
    {
      perror ("Multifind: shmdt.");
    }
  r = shmctl (smid, IPC_RMID, NULL);
  if (r == -1)
    {
      perror ("Multifind: shmCTL.");
    }
  cout << endl;
  cout << "-------------------------- End:" << (double) chrono::
    duration_cast < chrono::microseconds >
    (chrono::high_resolution_clock::now () -
     timeTestStart).count () / 1000000 << "s" << endl;
  return 0;
}
void
sygnal (int signo, siginfo_t * sigf, void *b)
{
  for (int i = 0; i < maxproc; ++i)
    {
      if (process[i].pid == sigf->si_pid)
    {
      process[i].countTimeProcess_stop = times (&bufor);
      i = maxproc;
    }
    }
  end_main += 1;
}
int
child_extern (int processnr, int shmid, unsigned long shmdiff,
          unsigned long shmlen, char *needleargv)
{
  const int needle_max = Dneedle_max;
  unsigned long child_len_max = shmlen;
  char *child_fileinmemory;
  char needle[needle_max];
  int needle_max_minusone = needle_max - 1;
  child_fileinmemory = (char *) shmat (shmid, NULL, 0);
  if (*child_fileinmemory == -1)
    {
      perror ("Onlyfind_str_in: shmat.");
      return 1;
    }
  child_fileinmemory += shmdiff;
  strcpy (needle, needleargv);
  char *endfileinmemory = child_fileinmemory + child_len_max - needle_max + 1;
  char *mark = child_fileinmemory;
  char *copymarkstart, *copymarkstop;
  char *needle_maxminus = needle + needle_max_minusone;
  char *needle_plus = needle + 1;
  unsigned long needle_max_minustwo = needle_max - 2;
  unsigned long copylen_max = child_len_max;
  char *mark_buff = mark;
  int count_buff;
  while ((mark = (char *) memchr (mark, *needle, copylen_max))
     && (mark < endfileinmemory))
    {
      if (mark[needle_max_minusone] == *needle_maxminus)
    {
      if (memcmp (++mark, needle_plus, needle_max_minustwo) == 0)
        {
          copymarkstart = mark;
          while (*(--copymarkstart) != (char) Dnew_line);
          copymarkstop = mark + needle_max;
          while (*(++copymarkstop) != (char) Dnew_line);
          mark = copymarkstop;
          count_buff = copymarkstop - copymarkstart;
          memmove (mark_buff, ++copymarkstart, count_buff);
          mark_buff += count_buff;
          copylen_max = endfileinmemory - mark;
          break;
        }
      else
        {
          --copylen_max;
        }
    }
      else
    {
      ++mark;
      --copylen_max;
    }
    }
  while ((mark = (char *) memchr (mark, *needle, copylen_max))
     && (mark < endfileinmemory))
    {
      if (mark[needle_max_minusone] == *needle_maxminus)
    {
      if (memcmp (++mark, needle_plus, needle_max_minustwo) == 0)
        {
          copymarkstart = mark;
          while (*(--copymarkstart) != (char) Dnew_line);
          copymarkstop = mark + needle_max;
          while (*(++copymarkstop) != (char) Dnew_line);
          mark = copymarkstop;
          count_buff = copymarkstop - copymarkstart;
          memcpy (mark_buff, ++copymarkstart, count_buff);
          mark_buff += count_buff;
          copylen_max = endfileinmemory - mark;
        }
      else
        {
          --copylen_max;
        }
    }
      else
    {
      ++mark;
      --copylen_max;
    }
    }
  int *size_buff = (int *) (child_fileinmemory + child_len_max - 8);
  *size_buff = mark_buff - child_fileinmemory;
  return (44);
}



***

Inne wpisy:



Update: 2018.07.17
Create: 2018.07.17

Brak komentarzy:

Prześlij komentarz