Commit fce1b140 authored by Jorand Jean-baptiste's avatar Jorand Jean-baptiste
Browse files

ajout pthread

parent 55bbe2de
Loading
Loading
Loading
Loading
+31
Changes for AdaptationParallel/include/statsgen.h: 31 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -21,6 +21,33 @@ struct Conteneur{
    Policy pol;	
};


struct thread_data {
	int thread_id;
	std::string filename;
	int lineBegin;
	int lineEnd;

	double total_counter = 0;
	double total_filter = 0;

	std::unordered_map<int, int> length;
	std::unordered_map<std::wstring, int> simplemasks;
	std::unordered_map<std::wstring, int> advancedmasks;
	std::unordered_map<std::wstring, int> charactersets;

	int mindigit = -1;
	int maxdigit = -1;
	int minlower = -1;
	int maxlower = -1;
	int minupper = -1;
	int maxupper = -1;
	int minspecial = -1;
	int maxspecial = -1;
};



class Statsgen {

public:
@@ -36,6 +63,7 @@ public:
	void setLimitAdvancedmask(int);

	int generate_stats(const std::string &);
	
	void print_stats();

private:
@@ -43,6 +71,7 @@ private:

	void updateMinMax(const Policy &);


	// Filters

	int hiderare = 0;
@@ -84,3 +113,5 @@ private:
Conteneur analyze_password(const std::wstring&);
void analyse_letter(const char &, char &, std::wstring &, std::wstring &, Policy &, int &, int &);
void analyse_charset(std::wstring &, const Policy &);

void * generate_stats_thread(void * );
 No newline at end of file
+1 −1
Changes for AdaptationParallel/src/main.cpp: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -84,7 +84,7 @@ int main(int argc,char* argv[]) {


	if (test.generate_stats(filename)) {
		test.print_stats();
		//test.print_stats();
	}
	
	
+125 −22
Changes for AdaptationParallel/src/statsgen.cpp: 125 added lines, 22 removed lines.
Original line number Diff line number Diff line
@@ -3,14 +3,17 @@
#include <algorithm>
#include <vector>
#include <regex>
#include <pthread.h>

#include "statsgen.h"
#include "utils.h"
#include "ThreadPool.h"
using namespace std;



#define NB_THREAD 2


/**********************************************************************
 *                           INITIALISATION                           *
 **********************************************************************/
@@ -231,48 +234,148 @@ void Statsgen::updateMinMax(const Policy & pol) {



int Statsgen::generate_stats(const string & filename) {
    wifstream readfile(filename);
    wstring line;


    ThreadPool pool(4);
    vector< future<Conteneur> > results;
void * generate_stats_thread(void * threadarg) {
    struct thread_data *my_data;
    my_data = (struct thread_data *) threadarg;

    wifstream readfile(my_data->filename);
    wstring line;
    int nbline = 0;

    while(readfile.good()) {
        ++nbline;
        getline(readfile, line);
        total_counter++;
        if (nbline < my_data->lineBegin) {
            continue;
        }
        if (nbline > my_data->lineEnd) {
            break;
        }



        
        my_data->total_counter++;
        if (line.size() == 0) {
            wcout << "Error empty password, line " << total_counter << endl;
            wcout << "Error empty password, line " << my_data->total_counter << endl;
            continue;
        }

        total_filter++;
        Conteneur c = analyze_password(line);
        
        results.emplace_back(
            pool.enqueue([line] {
                return analyze_password(line);
            })
        );
        my_data->total_filter++;

        my_data->length[ c.pass_length ] += 1;
        my_data->charactersets[ c.characterset ] += 1;
        my_data->simplemasks[ c.simplemask_string ] += 1;
        my_data->advancedmasks[ c.advancedmask_string ] += 1;
        
        if (my_data->mindigit == -1 || my_data->mindigit > c.pol.digit) {
            my_data->mindigit = c.pol.digit;
        }
        if (my_data->maxdigit == -1 || my_data->maxdigit < c.pol.digit) {
            my_data->maxdigit = c.pol.digit;
        }

    for(auto && result: results) {
        Conteneur c = result.get();
        stats_length[ c.pass_length ] += 1;
        stats_charactersets[ c.characterset ] += 1;
        stats_simplemasks[ c.simplemask_string ] += 1;
        stats_advancedmasks[ c.advancedmask_string ] += 1;
        updateMinMax(c.pol);
        if (my_data->minlower == -1 || my_data->minlower > c.pol.lower) {
            my_data->minlower = c.pol.lower;
        }
        if (my_data->maxlower == -1 || my_data->maxlower < c.pol.lower) {
            my_data->maxlower = c.pol.lower;
        }

        if (my_data->minupper == -1 || my_data->minupper > c.pol.upper) {
            my_data->minupper = c.pol.upper;
        }
        if (my_data->maxupper == -1 || my_data->maxupper < c.pol.upper) {
            my_data->maxupper = c.pol.upper;
        }

        if (my_data->minspecial == -1 || my_data->minspecial > c.pol.special) {
            my_data->minspecial = c.pol.special;
        }
        if (my_data->maxspecial == -1 || my_data->maxspecial < c.pol.special) {
            my_data->maxspecial = c.pol.special;
        }
    }

    readfile.close();

    pthread_exit(NULL);
}


int nbline_file(const string & filename) {
    wcout << "debut" << endl;
    wifstream readfile(filename);
    wstring line;
    int nb = 0;

    while(readfile.good()) {
        getline(readfile, line);
        ++nb;
    }

    wcout << "fin" << endl;
    return nb;
}





int Statsgen::generate_stats(const string & filename) {
    int nbline = nbline_file(filename);
    wcout << nbline << endl;
    int rc;
    int i;
    pthread_t threads[NB_THREAD];
    struct thread_data td[NB_THREAD];

    pthread_attr_t attr;
    void *status;

   // Initialize and set thread joinable
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

    for( i = 0; i < NB_THREAD; i++ ) {
        td[i].filename = filename;
        td[i].thread_id = i;
        td[i].lineBegin = i*(nbline/NB_THREAD);
        td[i].lineEnd = (i+1)*nbline/NB_THREAD;

        wcout << i << " : " << td[i].lineBegin << " --> " << td[i].lineEnd << endl;
        rc = pthread_create(&threads[i], NULL, generate_stats_thread, (void *)&td[i] );
      
        if (rc) {
            cout << "Error:unable to create thread," << rc << endl;
            exit(-1);
        }
    }

   // free attribute and wait for the other threads
   pthread_attr_destroy(&attr);
   for( i = 0; i < NB_THREAD; i++ ) {
      rc = pthread_join(threads[i], &status);
      if (rc) {
         cout << "Error:unable to join," << rc << endl;
         exit(-1);
      }
   }




    /*
    if (!total_counter) {
        wcerr << "Empty file or not existing file" << endl;
        return 0;
    }
    */

    readfile.close();
    return 1;
}